在Lua函数调用中从Lua中获取结果返回的LuaJava

如何在 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 调用?

原文链接 https://stackoverflow.com/questions/8611948

点赞
stackoverflow用户234175
stackoverflow用户234175

这样做是否行得通?

int top_index = luaState.getTop();
double result = luaState.isNumber(top_index) ?
                luaState.toNumber(top_index) : 0.0;
2011-12-23 06:21:51