Go back to Maciej's projects

C++ Multi Script Library (Test)

Download the source code (930 KB)

12 February, 2008 (Update)
Added support for ocaml language.

Unfortunately only function binding is supported and I'm not planning to add support for C++ class binding as it would require
a lot of hacking with Ocaml source code. The main problem is that Ocaml doesn't support class binding at all, only functions.
Would be nice to see this feature in future Ocaml releases, but I doubt it's gonna come.

The binding with Ocaml was a little bit more difficult to do compared to lua, squirrel and game monkey.
I actually had to modify the source code of the compiler, especially to support "do string" concept without having to restart ocaml environment
each time the script is run.

I also had to do some nasty work to allow Ocaml dynamically bind new C functions in run-time which is normally not supported at all.

The new source code contains necessary part of the Ocaml interpreter (modified by myself). To run Ocaml scripts you also need to install
the Ocaml binary distribution from Inria site - it's necessary to compile (not execute) the scripts.

Note that the code there is just to demonstrate the idea, it's far from optimal way of doing things.
25 January, 2008 (First Release)
General Overview
This is a small test I did to see how easy it would be to have common C++ side interface to various scripting languages.
After I've done this I thought someone might be interested in that as well, so I put it in the form of a library on this web-page.

This library contains support for three different - but similar - scripting languages:
lua
squirrel
game monkey
As a side note - all of these languages are typeless, garbage collected ones. Only squirrel has native support for classes.

The library shows how same interface is used to register and use C++ classes and functions from both script and C++ sides.

Library
The library contains two key interfaces that have to be implemented for each scripting language:
ScriptContext which plays the role of a virtual machine and...
ScriptStack which is an interface used to make calls - both calls to C++ functions registered in script and calls to script functions
The library supports both garbage collected and non-garbage collected classes.

It was meant to be useful for game engine where entities can be created, accessed and destroyed from both script and C++ sides.

Test App
There is a small test app included. What it does it runs all tests for all supported scripting languages.
I recommend you first take a look at the sample to understand the basic idea of the library.

Source code
I include the full source code of the library as well as necessary source code of each scripting language - lua, squirrel and game monkey.
This is why the download package is so big. I also include VC++ projects (MSVC 8.0). The library is not platform independent, but it could
really easily be ported to any platform. Please note that it was done as a quick test.

If you found a bug or have any suggestions please let me know.