简体   繁体   English

Lua 设置默认错误处理程序

[英]Lua set default error handler

The default lua_pcall error handler (as of Lua 5.3) does nothing, letting the exception message remain on top of the stack.默认的lua_pcall错误处理程序(自 Lua 5.3 起)不执行任何操作,让异常消息保留在堆栈顶部。 We would like to change this so we get a luaL_traceback traceback in addition to the exception message on top of the stack on lua_pcall failure.我们想更改此设置,以便除了在lua_pcall失败时堆栈顶部的异常消息之外,我们还可以获得luaL_traceback回溯。

Unfortunately I think that means we need to insert the error handler just below all of our pcalls.不幸的是,我认为这意味着我们需要在所有 pcalls 的正下方插入错误处理程序。 The most robust way of doing this seems to be like this:最可靠的方法似乎是这样的:

/* push function and arguments */
lua_pushstuff...

/* push error handler */
lua_pushcfunction(L, my_error_handler);

/* move the error handler just below the function and arguments */
lua_insert(L, -(number of args + 1));

if (lua_pcall(L, nargs, nresults, -(number of args + 1))) {
    /* error here (my_error_handler was invoked) */
    /* do something with lua_tostring(L, -1) */
}

/* afterwards, pop the error handler (it may be below return values) */
lua_pop(L, 1);

But this introduces noise at each pcall (we have quite a few, since we have some Lua callbacks called asynchronously from C) and feels kind of repetitive.但这会在每个 pcall 中引入噪音(我们有很多,因为我们有一些从 C 异步调用的 Lua 回调)并且感觉有点重复。 I thought this could be wrapped inside some lua_mypcall function that does this setup work automatically, but I have two questions:我认为这可以包含在一些lua_mypcall function 中,它会自动执行此设置,但我有两个问题:

  1. is this approach liable to break with more complex stack manipulation before (or inside) the pcall?这种方法是否容易在 pcall 之前(或内部)破坏更复杂的堆栈操作? (I'm not super well versed in the Lua stack yet) (我还不是很精通 Lua 堆栈)

  2. since we'd like the traceback on the majority of pcalls, it makes sense to make this error handler the default, and having the previous error handler (that does nothing) be specified manually, so is there a way to globally change the default error handler for the Lua state?由于我们希望对大多数 pcalls 进行回溯,因此将此错误处理程序设为默认值并手动指定先前的错误处理程序(什么都不做)是有意义的,所以有没有办法全局更改默认错误Lua state 的处理程序?

I saw that lua_pcallk has some code for errfunc == 0 , but it doesn't seem configurable.我看到lua_pcallk有一些errfunc == 0的代码,但它似乎不可配置。 We could hack the Lua implementation to change the default manually, but would like to avoid that.我们可以破解 Lua 实现以手动更改默认值,但希望避免这种情况。

We are using Lua 5.3.我们正在使用 Lua 5.3。 Thanks.谢谢。

Your basic approach is sound, but you are missing a lua_remove (instead of lua_pop ) and your stack indices are wrong. 您的基本方法是合理的,但是您缺少lua_remove (而不是lua_pop ),并且堆栈索引错误。 Try this: 尝试这个:

int lua_mypcall( lua_State* L, int nargs, int nret ) {
  /* calculate stack position for message handler */
  int hpos = lua_gettop( L ) - nargs;
  int ret = 0;
  /* push custom error message handler */
  lua_pushcfunction( L, my_error_handler );
  /* move it before function and arguments */
  lua_insert( L, hpos );
  /* call lua_pcall function with custom handler */
  ret = lua_pcall( L, nargs, nret, hpos );
  /* remove custom error message handler from stack */
  lua_remove( L, hpos );
  /* pass return value of lua_pcall */
  return ret;
}
if (lua_pcall(L,0,0,0)!=LUA_OK) fprintf(stderr,"%s\n", lua_tostring(L,-1) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM