What languages generates bytecodes and can be load/executed in C++

1.6k Views Asked by At

I'd like to know what languages fit this scenario:

I'm writing an application (C++) and I need to add some type of scripting support to it, but this language must be able to be "compiled".

This compilation will generate some type of "bytecode" that is not readable by humans. Then I need to be able to get this bytecode, load it inside the C++ application and execute it.

Exporting c++ application functions to the script and calling back script functions is a must.

The main idea is to extend application functionality without touching the C++ code, but it must be hard for a user to read these scripts.

What languages can I use?

5

There are 5 best solutions below

1
On

I'd love to suggest ChaiScript, but we don't have any kind of encryption or bytecode support built in.

That said, there's no reason you could not use any existing C++ scripting engine. ChaiScript, luabind, etc, could all be used if you provide your own encryption and decryption functions for feeding the script into the engine.

The one scripting engine that I know of that supports c++ and bytecode and is relatively obscure is AngelScript. They have a page on loading pre-compiled bytecode.

1
On

Maybe Vox* can fit your bill?

The scripts can be precompiled into portable bytecode, and is very easy to embed, with stackbased API similar to Lua:

// most basic embedding example
VoxVM* v = vox_open(1024);
vox_aux_seterrorhandlers(v);
vox_pushroottable();
vox_dofile(v, "somescript.vx", false, true);
vox_pop(v, 1);
vox_close(v);

For a more fully featured example, see etc/minimal.cpp and src/frontend/frontend.cpp


* Vox is a project that i've been working on to replace Lua for personal purposes. It's based on a highly modified version of the VM of squirrel3 (but is not compatible with squirrel anymore!).

The syntax has also changed a little: Instead of using <- for new slots, it uses :=, to avoid ambigious syntax.

The Core is finished and ready for use in embedded projects, but the stdlib is still unfinished, since Vox is a very young project (due to that, documentation is currently lacking, but the stdlib is a good example for embedded programming, as well as the examples for general purpose scripting).

3
On

I would suggest you use LLVM. LLVM's intermediate representation can be stored in bytecode format. You will need to use an existing front-end for a scripting language or develop your own front-end. Your C++ application can load the bytecode file and JIT it. Here is a list of existing projects based on LLVM. You might find there a solution which fits your needs.

1
On

Perhaps Brainfuck? Certainly easy to find interpreters, and much less readable than your average machine code or byte code.

0
On

LuaJIT is a nice, quick Lua interpreter, that JITs Lua into native machine code. LuaJIT bytecode is platform independent, unlike regular Lua.

Take a look for yourself: http://luajit.org/luajit.html http://luajit.org/extensions.html