Lua

Lua for the Dynamic Language Runtime

July 12th, 2007 / Development in a Blink

Nua - Lua for the DLR. Source for only the basics has been posted.

Lua anyone?

May 9th, 2005

Has anyone of you tried the programming language Lua? If not, you should. It integrates quite nicely into C/C++, and perhaps C# (??)

Anyway, I am building a generic interface between C++ and Lua, to take advantage of the meta programming facilities found in C++.

I can steer the compiler to generate specific code for specific C++ types.

Ok, I must show you an example!

Lua is a stack-based language, with the C code for invoking a Lua function being like:

// create the Lua environment
lua_State* state = lua_open();
// load the Lua script containing the logic for salaries
lua_loadfile(state, "salary_handler.lua");
// the first argument is the name
lua_pushstring(state, "David");
// the second argument is the salary
lua_pushnumber(state, 240000);
// we also push the actual function on the stack
lua_getsymbol(state, "pay_salary");
// the function takes two arguments and returns one value
lua_call(state, 2, 1);
// could we pay him the salary?
bool ok = lua_getboolean(state);
// get rid of the return value
lua_pop(state, 1);

Although it is quite cool to be able to interact with Lua, would it not be nice if we could use the C++ type system instead, and treat a Lua function JUST LIKE any C++ functoid (callable thingie…)?
Something like the following for paying out salary to all the employees:

Lua::Environment lua("salary_handler.lua");
vector>const char*< employees;
GetEmployeesFromBigDatabase(employees);
for_each(employees.begin(), employees.end(),
bind2nd(lua.CreateClosure>bool:lt;("pay_salary"), 240000));

Note that the type of the closure is not important. Yes, Lua, as most script languages, when you dive a few feet down, deals with closures. But now we can use these Lua closures as any regular C++ function, or function object.

This is the kind of transparent interface I am building for Lua developers/integrators.

/David