Lua Getting Result Back in LuaJava from Lua function call

一笑奈何 提交于 2019-12-11 13:20:27

问题


How does one get the value back from a Lua function call in LuaJava.

Lets say I have calc.lua:

function foo(n) return n*2 end

I call the function in Java as follows:

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);

Now what do I have to call on LuaState object to get the result of this last function call foo(5)?

Is there an example somewhere showing Java->Lua invocation with return values from the call?


回答1:


Would something like this do the trick?

int top_index = luaState.getTop();
double result = luaState.isNumber(top_index) ? 
                luaState.toNumber(top_index) : 0.0;


来源:https://stackoverflow.com/questions/8611948/lua-getting-result-back-in-luajava-from-lua-function-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!