LuaJava为LuaState.pcall(a,b, error_function_index)设置错误处理程序?
我正在尝试调用:
LuaState.pcall(num_args,num_returns, error_handler_index).
我需要知道如何为此函数设置错误处理程序。事实上,我认为如果有人展示如何调用 Lua 函数并使用 LuaJava 获取数值结果就好了。这可能会节省大量时间和问题。我正在寻找但没有找到错误函数的签名,以及如何将其放置在 LuaState 堆栈上的正确位置。所有 Java->Lua 示例要么打印一个不返回的值,要么在使用 Lua 传入的 Java 对象上设置值。我想看看如何直接调用 Lua 函数并返回结果。
更新:一种解决方案是使用 LuaState.pcall(1,1,0) 不传递任何错误处理程序,方法是为错误处理程序传递零:
String errorStr;
L.getGlobal("foo");
L.pushNumber(8.0);
int retCode=L.pcall(1,1,0);
if (retCode!=0){
errorStr = L.toString(-1);
}
double finalResult = L.toNumber(-1);
其中 calc.lua 已加载:
function foo(n)
return n*2
end
现在还有一种方法可以设置错误处理程序吗?谢谢
I am trying to call:
LuaState.pcall(num_args,num_returns, error_handler_index).
I need to know how to set the error handler for this function. In fact, I think it would be nice it someone showed how to invoke a Lua function and get a numerical result back using LuaJava. This might save a lot of time and questions. I am looking but not finding the signature for the error function, and how to place it at the right point on the LuaState stack. All the Java->Lua examples are either printing a value with no return or they are setting values on a Java object passed in using Lua. I would like to see how to call a Lua function directly and get the result back.
Update: one solution is to pass no error handler using LuaState.pcall(1,1,0) by passing zero for the error handler:
String errorStr;
L.getGlobal("foo");
L.pushNumber(8.0);
int retCode=L.pcall(1,1,0);
if (retCode!=0){
errorStr = L.toString(-1);
}
double finalResult = L.toNumber(-1);
where calc.lua has been loaded:
function foo(n)
return n*2
end
Now is there a way to set an error handler as well? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您还想要堆栈回溯(我相信您会这样做:),您可以将
debug.traceback
作为错误函数传递。看看 它是如何在 AndroLua 中实现的。基本上,您必须确保堆栈设置如下:
debug.traceback
)您可以通过示例执行此操作:
If you also want the stack traceback (I'm sure you do :), you can pass
debug.traceback
as the error function. Take a peek at how it's implemented in AndroLua.Basically, you have to make sure your stack is set up as follows:
debug.traceback
)You can do it like this with your example:
假设您有一个 Lua 函数来处理错误:
您可以将该 err_handler 函数传递到您的 pcall 中:
Assuming you have a Lua function somewhere to handle the error:
You can pass that
err_handler
function into your pcall: