LuaJava为LuaState.pcall(a,b, error_function_index)设置错误处理程序?

发布于 2024-12-23 02:41:23 字数 700 浏览 1 评论 0原文

我正在尝试调用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放飞的风筝 2024-12-30 02:41:23

如果您还想要堆栈回溯(我相信您会这样做:),您可以将 debug.traceback 作为错误函数传递。看看 它是如何在 AndroLua 中实现的

基本上,您必须确保堆栈设置如下:

  • 错误处理程序 (debug.traceback)
  • 您要调用的
  • 函数 参数

您可以通过示例执行此操作:

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...

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:

  • Error handler (debug.traceback)
  • Function you want to call
  • Parameters

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...
放我走吧 2024-12-30 02:41:23

假设您有一个 Lua 函数来处理错误:

function err_handler(errstr)
  -- exception in progress, stack's unwinding but control 
  -- hasn't returned to caller yet
  -- do whatever you need in here
  return "I caught an error! " .. errstr
end

您可以将该 err_handler 函数传递到您的 pcall 中:

double finalResult;

L.getGlobal("err_handler"); 
L.getGlobal("foo");
L.pushNumber(8.0);

// err_handler, foo, 8.0
if (L.pcall(1, 1, -3) != 0)
{
    // err_handler, error message
    Log.LogError( L.toString(-1) );  // "I caught an error! " .. errstr
}
else 
{
    // err_handler, foo's result
    finalResult = L.toNumber(-1);
}
// After you're done, leave the stack the way you found it
L.pop(2);

Assuming you have a Lua function somewhere to handle the error:

function err_handler(errstr)
  -- exception in progress, stack's unwinding but control 
  -- hasn't returned to caller yet
  -- do whatever you need in here
  return "I caught an error! " .. errstr
end

You can pass that err_handler function into your pcall:

double finalResult;

L.getGlobal("err_handler"); 
L.getGlobal("foo");
L.pushNumber(8.0);

// err_handler, foo, 8.0
if (L.pcall(1, 1, -3) != 0)
{
    // err_handler, error message
    Log.LogError( L.toString(-1) );  // "I caught an error! " .. errstr
}
else 
{
    // err_handler, foo's result
    finalResult = L.toNumber(-1);
}
// After you're done, leave the stack the way you found it
L.pop(2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文