lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Patrick, I started working on this last fall (2010), and only recently decided to publicize it. Thanks for pointing out your project, always good to see alternative ways of doing things. 

when you need to use Lua from C++ without exposing
C function in Lua (one-way), it should be enough to be able to
evaluate arbitrary Lua expressions, passing around arguments and
return values. 
 
For me the above wasn't enough. I needed ways to keep references to compiled chunks of Lua code so that calling them often didn't require recompiling strings of code over and over. Also needed to sandbox the scripting environment, and easily load DLL's.  As explained in the docs for lua-icxx, I could not find any other lib that did that. 

I also really like how LuaTempResult gives access to any number of return values on the stack, with automatic type inference (for basic types). Potentially some conversion functions could also be defined for UDT, but that's slipping into domain of supporting UDT natively in Lua-icxx, I rather combine lua-icxx with SWIG (or other) for that (which is what I actually do).

Here is how some of your example code from LuaClassBasedCall would look like with Lua-icxx (have not had time to run this, so may need some tweaking):

LuaInterpreter L; 
L.doString("print('Hello world')"); 

LuaTableRef myTable = L.eval("mytable={true, 0, 3.1416, 0, 'world'}; return mytable");
// only dict support right now:
myTable[2] = 2; // replaces the first zero in mytable
myTable[4] = "Hello"; // no wide char support right now

LuaTempResult res = L.eval("for k,v in pairs(mytable) do print(k,v,type(v)) end");
if ( ! res.isOK() )
    cout << "Lua error : %s\n" << res.errMsg();

// get table items
bool   res1 = myTable[1];
double res2 = myTable[2]; 
float  res3 = myTable[3];
string res4 = myTable[4]; // no wchar support yet
string res5 = myTable[5];
printf("mytable = { %d, %g, %g, %S, %s }\n", res1, res2, res3, res4, res5);

LuaFuncRef sumFn = L.chunkFromString("local a,b,c=...; return a+b+c");
double sum = sumFn(2.3, 4, 3.1416);     // figures out all types
string pi  = sumFn("3.1", "4", "16");   // no recompilation
printf("The sum is %g\n", sum);

// Outputs example:
LuaTempResult vals = L.eval("s='P\\0Q\\0R'; u=io.stdin; return s,u,{1,2,3}");
string aString   = vals[0];
LuaObjRef stdin  = vals[1]; // opaque pointers only; combine with SWIG for more
LuaTableRef a123 = vals[2];

// Custom types: combine with SWIG or tolua++ or other
LuaTempResult modLoaded = L.openDynLib( "your_lib", "luaopen_your_lib" );
if ( ! modLoaded.ok() )
    throw runtime_error("FATAL error: could not load Lua extension "your-lib"");
LuaClassObjRef yourObj = L.eval("yourObj"); // assume your_lib created a global
yourObj.callMethod("sayHello", "world"); // from Lua: yourObj:sayHello("world")

The above shows how different the two libraries are. And the above does not show off several important capabilities of Lua-icxx like sandboxing. Sure, Lua-icxx doesn't currently support wchar, or STL containers, but these just haven't been requirements; if other users really like to use Lua-icxx and there is strong demand for such things then there is no reason they couldn't be added.  

Oliver