joi, 22 iulie 2010

Lua2C, an updated version

I know I have been "missing in action" lately but I am working furiously, and I seem to have too little time for my blog (very sad face). But, just for a breath of fresh air, I thought I'd share something with the world.

Entering lua2c.lua


Lately I became quite interested in Lua (a lot actually). It has phenomenal speed, exceptional interfacing with C and some features and libraries that just make my day (i.e. coroutines, lpeg, lua-ev and others), and since I needed to embed some Lua scripts (entirely) in a C project I'm currently working on, I ended up adapting Mike Edgar's "bin2c.lua" script (which takes a Lua script and turns it into a C header file) to suit my needs.

Basic functionality


Specifically, this adaptation generates a function that takes a Lua state as the only argument and then runs the embedded Lua code in the given state after which it returns the status (as opposed to putting the code straight in the top-level scope of the generated file). This makes it easier to embed code in C and then invoke it, and also to apply the same code onto multiple Lua states (e.g. multiple threads).

Check the end of the post for a usage sample.