Lua Getting Result Back in LuaJava from a Lua Function Call

How to get value back from calling Lua function in LuaJava.

Let's 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 need to call for the LuaState object to get the result of this last function call to foo (5)?

Is there an example showing a Java-> Lua call with return values ​​from a call?

+1
source share
1 answer

Would something like this do the trick?

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

Source: https://habr.com/ru/post/1387503/


All Articles