|
I am just having a look at Kahlua but I cannot get the basic
things to work. I am trying to call one of my Java methods from within Lua
and my code looks like this: final LuaState state = new LuaState(System.out); UserdataArray.register(state); OsLib.register(state); LuaCompiler.register(state); final LuaTable john = new LuaTableImpl(); state.getEnvironment().rawset("john", john); john.rawset("m1", this); john.rawset("__index", john); state.setUserdataMetatable(KahluaTest.class, john); try { final LuaClosure closure =
LuaCompiler.loadstring("function doIt() john.m1(42) end", "doIt", state
.getEnvironment()); final Object runTest = state.getEnvironment().rawget("doIt"); BaseLib.luaAssert(runTest != null, "Could not
find doIt"); final Object[] results = state.pcall(runTest, new Object[] {closure}); } catch (final Exception e) { KahluaTest.logger.error(e); } Where “john” is the name of the function library
and “m1” is the function I am trying to call. Well, it never
gets to even try to call it as the code above fails on the line where
BaseLib.luaAssert() (5 lines from the bottom) is being called to check
for the existence of the function doIt. No errors are returned from the Lua compiler so why can’t
it find “doIt” in the environment? As you can probably tell,
I don’t know much about Lua as I have not used it before. Thanks, John |