Implementing a scripting engine in C ++

I am learning how best to extend a C ++ application with scripting capabilities, and I am considering either Python or JavaScript. User scripts must have access to the application data model.

Do you have experience implementing these script engines? What are the possible pitfalls?

+4
source share
5 answers

It is probably easy to embed Python using the Boost :: Python library (fine, good, sarcasm.) Nothing is “easy” when it comes to cross-language functionality. Boost has done a lot to facilitate this development. One of the developers I worked with swears on the Boost-> Python interface. Its code can be programmed by the user in Python, with REPL embedded in the user interface. Amazing

However, my experience has been better observed using SWIG and other languages ​​such as Java. I am currently working with SWIG to port C ++ to Python. There are all kinds of errors with exceptions, threads, polymorphism in different languages, etc.

First I will look at these two places. As I said, nothing will be “easy”, but both of them make life more liveable.

+6
source

Lua is also an excellent candidate for implementation in programs. His identity, and even his native system of cross-languages, is not bad.

For JavaScript, it’s best now to look at V8 (from Google), which is easy to work with.

+7
source

If you are not really configured for Python or Javascript, I would consider using Lua . Since it is fully designed as a built-in scripting engine, it completely covers the fact that C and C ++ are already doing well. It is also pretty easy to embed while you only interact between your code and the Lua engine in terms of functions called by C.

If you want to use the C ++ level interface, you can take a look at LuaBind , which allows the Lua Class derived from the (created by the proxy server) C ++ class that you wrote.

+4
source

Boost :: Python, as in wheat's answer, is a very mature solution.

Lua's reputation is easy to build in, but I haven't tried it myself.

As a user of R , I'm more interested in embedding R , which is possible with the RInside package. A simple example:

#include <RInside.h> // for the embedded R via RInside int main(int argc, char *argv[]) { RInside R(argc, argv); // create an embedded R instance R["txt"] = "Hello, world!\n"; // assign a char* (string) to 'txt' R.parseEvalQ("cat(txt)"); // eval the init string, ignoring any returns exit(0); } 

and there are a few more examples in the package. RInside essentially provides you with a good wrapper around the R mechanism using some of Rcpp .

+1
source

Look at the angel script simple and easy to embed, c / C ++, as the syntax. free and cors platform. u can start in a few hours.

+1
source

Source: https://habr.com/ru/post/1310883/


All Articles