A coder's thoughts

Ideas from a coding, music and electronics lover.

Using Lua within .NET programs

Lua is a small language know for its flexibility and use in the video games industry. It is implemented in fast, small C code which makes it a piece of cake to embed in host programs. Not to mention it is largely portable.

Of course, us programmers like the comfortable .NET Framework as it provides full garbage collection, lots of libraries for everything, reflection. I mean, you don’t have such things in C, or even in C++. Except for the libraries, but that’s another story. However, embedding Lua in a .NET application is not as fast as doing so in a C program. You would have to use a lot of P/Invokes or to write a whole wrapper using C++/CLI. Or, you have LuaInterface, which provides nice functionality with some sugar syntaxes, which is nice. But you always have to carry your lua52.dll everywhere you go, and that’s not as nice. Also, it makes porting your app on Mono a mess. It would have been great to have a nice, easy to use managed implementation of Lua, isn’t it?

Enter NetLua.
NetLua is exactly that. It is a completely rewritten implementation of the Lua language for the .NET platform. It uses modern dynamic techniques to make its use fluent and easy.

using NetLua;

static LuaArguments print(LuaArguments args)
    foreach(LuaObject obj in args)
        Console.Write("{0}\t", obj.ToString());

static void Main()
    var lua = new NetLua.Lua();
    lua.DynamicContext.print = (LuaFunction)print;
    lua.DoString("a = 4; print(a)");

Pretty, isn’t it? NetLua fully supports all language control structures and syntax (apart from parentheses-less function calls and debug library. The basic functions are supported but no other libraries are implemented (though there is a math.* and io.* implementation in development) as they are meant to be implemented by the user for its own use.

Anyway, NetLua is still in his first steps of development and should not be considered ready to use in a production environment. You might still want to keep an eye on it: the project is stored on GitHub and, obviously, everyone is free to help me in its development! NetLua is released under the MIT License.


blog comments powered by Disqus