Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I embed a scripting language? [closed]

Tags:

c++

python

lua

I am learning C++ now and I have heard a lot about embedded scripting languages. I imagined it completely different.

I thought I would write all of my performance heavy functions in C++ and call them out of a scripting language such as Lua or Python.

But it seems it is the other way around. -> Write functions in Lua/Python and call them in C code.

What is the advantage of embedding a language in C++ instead of writing an API in C++ and calling those functions in another language?

Example:

// function in c++
int expensiveFunction(){
  return 1;
}

Then in Python I would call this function and I would have the performance from C++ but can make changes at runtime thanks to Python's runtime interpreter.

like image 763
Maik Klein Avatar asked Nov 05 '12 23:11

Maik Klein


1 Answers

Actually, a lot of game engines like to build interfaces to the engine by embedding Lua or Python. There are advantages to this:

  • Non-programmers can interface with the engine.
  • You do not need to recompile for minor script changes.
  • Errors in the script might not crash the entire system.

C++ is quite useful as a backend for projects that want the flexability of scripting languages, but want the performance of C++. I have not heard of projects that use C++ as a frontend, with a scripting language as the backend.

API Style

We use this style in my company's software. We expose an API through a Windows DLL that can be called by most languages fairly easily. We specifically support VB and VBA. This is great when the backend is from outside the script maker's control. However, it is hard to debug issues that arise from the script maker's perspective.

Advantages

  • Strong decoupling
  • Accessible from different languages

Disadvantages

  • Hard to debug 2 processes

Embedded Style

The software actually embeds the script interpreter into the software. This way you can expose features as if they were native functions. In this style, the script makers and the backend programmers typically are in the same company. It can also be used by traditional software to allow others to extend the functionality of the application. If they share source code, you can debug problems arising from scripts in a much easier manner. The application also takes care of when and how to launch your scripts. However, in order to support additional languages, the application developer has to embed other interpreters.

Advantages

  • Stronger coupling
  • Easier to debug one process

Disadvantages

  • Only accessible through the approved language
like image 117
Michael McGuire Avatar answered Nov 15 '22 22:11

Michael McGuire