LuaJava为LuaState.pcall(a,b,error_function_index)设置错误处理程序吗? [英] LuaJava Setting Error Handler for LuaState.pcall(a,b, error_function_index)?

查看:218
本文介绍了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函数并返回结果.

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.

更新:一种解决方案是使用LuaState.pcall(1,1,0)通过不向错误处理程序传递零来不传递错误处理程序:

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

已加载calc.lua的位置:

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

推荐答案

如果您还想要堆栈回溯(我敢肯定,请执行:),则可以将debug.traceback作为错误函数传递.看看如何在AndroLua中实现.

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:

L.getGlobal("debug");
L.getField(-1, "traceback");      // the handler
L.getGlobal("foo");               // the function
L.pushNumber(42);                 // the parameters
if (L.pcall(1, 1, -3) != 0) { ... // ... you know the drill...

这篇关于LuaJava为LuaState.pcall(a,b,error_function_index)设置错误处理程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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