Lua从Lua函数调用返回LuaJava中的结果 [英] Lua Getting Result Back in LuaJava from Lua function call

查看:69
本文介绍了Lua从Lua函数调用返回LuaJava中的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从LuaJava中的Lua函数调用中获取值.

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

让我说我有calc.lua:

Lets say I have calc.lua:

function foo(n) return n*2 end

我用Java调用该函数,如下所示:

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

现在我必须在LuaState对象上调用什么才能获取上一个函数调用foo(5)的结果?

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

是否有一个示例显示Java-> Lua调用以及调用中的返回值?

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

推荐答案

这样的技巧会起作用吗?

Would something like this do the trick?

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

这篇关于Lua从Lua函数调用返回LuaJava中的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆