如何从LuaJava中的Lua函数调用中获取值。
让我说我有calc.lua:
function foo(n) return n*2 end
我用Java调用函数如下:
LuaState luaState;
this.luaState = LuaStateFactory.newLuaState();
this.luaState.openLibs();
this.luaState.LdoFile("calc.lua");
this.luaState.getGlobal("foo");
this.luaState.pushNumber(5.0);
int retcode=this.luaState.pcall(1, 1,0);
现在我需要调用LuaState对象来获取最后一个函数调用foo(5)的结果?
是否有一个示例显示Java-> Lua调用以及来自调用的返回值?
答案 0 :(得分:2)
这样的事情能做到吗?
int top_index = luaState.getTop();
double result = luaState.isNumber(top_index) ?
luaState.toNumber(top_index) : 0.0;