I'm trying to get a simple function working in LuaJava (adding two numbers). I have very little experience with Lua, and am finding this difficult as I haven't been able to find in depth documentation for LuaJava. I can currently print out to the java console from lua, but nothing beyond that.
I have tried implementing a few methods, but get the same error every time:
PANIC: unprotected error in call to Lua API (attempt to call a nil value)
This is the code I am using:
import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;
public class Hello {
public static void main(String[] args) {
LuaState l = LuaStateFactory.newLuaState();
l.openLibs();
l.LdoFile("main.lua");
l.call(0, 0);
l.getGlobal("add");
l.pushInteger(1);
l.pushInteger(1);
l.call(2, 1);
int result = l.toInteger(1);
l.pop(1);
System.out.println("1 + 1 = " + result);
}
}
And the Lua file:
function add(a, b)
return a + b
end
My IDE is Eclipse. Thanks in advance for any help.
I am going to assume the problem is occurring on this line l.call(0, 0);
.
l.LdoFile("main.lua");
does not, most likely, return a function on the stack that must be called. That is what the lua C function luaL_loadfile
does. luaL_dofile
is a macro around luaL_loadfile
which also runs the returned chunk function.
So after l.LdoFile("main.lua");
finishes I believe you have nothing on the stack and your add
function is available for use.