One of my current projects has gained the critical mass complexity that can justify an embedding of a scripting language, so that some parts of the projects can be flexibly scripted instead of being hardcoded into the C++ code and thus require a time-consuming compilation every time anything changes.
After reading a little about this, I found out that there are a lot of options, and most of them are really easy!
My first idea was to have two different programs, one in C++ and the other one in PHP (but any other multipurpose script language would suffice), and make them communicate with each other. The best choice for communication would the TCP/IP sockets, and interface could be coded pretty easily. Each function that needed to be exported, could easily be mapped through the network code. Two different programs could even be located on different machines and easily connect through the net.
This solution is possible, and is not that bad actually, but it can get pretty annoying to have two different programs to run and maintain.
So there I came upon the Real solution.
Lua is an opensource scripting language, which is very easy to embed and is being used worldwide mostly in games to script the gaming logic. It’s biggest advantage is the speed. By checking different benchmarks, I found out that lua code can be just around 30 times slower than c++ code, which is VERY fast for a scripting language. (For comparison, php is like 300-400 times slower, perl is something like 100-300 times, and Java approx. 10 times).
With this speed, some pretty complex calculations could be easily done in lua, instead of being hardcoded. The other advantage is that lua is semantically extensible, which means that it has very few features out of the box, but you could easily implement almost any paradigm or design pattern, whichever is most suitable for your application.
For example, there are no classes in their usual meaning in lua, they are instead “implemented” in code. When using lua, you are not only writing programs, but also sort of create your own language as you go.
As for embedding, with a small additional opensource library (of mostly macros) Luabind, binding functions and classes of your c++ program to lua becomes a piece of cake. It almost comes down to 1 extra line of code per mapped function, really smooth.
The downside is the speed (approx. 300-400 times slower than native) and the complexity. Also, since the language itself is much more complex than barebone lua, embedding it to applications and mapping functions and classes from c++ can sometimes be tricky and bloated with type conversions and argument checks.
So this seems to be the pro way to do it, so that’s what I am gonna do. (This time it’s gonna be lua but I am eager to try the spidermonkey some time also).
PS. It’s a pity I have overlooked this at first, but apparently, boost (which you should use most of the time anyway, cz its awesome) has built-in python bindings which work almost as easy (sometimes even easier) than luabind. So that’s a great alternative also, especially if you are a python fan.