默认的lua_pcall
错误处理程序(从Lua 5.3开始)不执行任何操作,使异常消息保留在堆栈的顶部。我们希望对此进行更改,以便luaL_traceback
在lua_pcall
发生故障时,除了堆栈顶部的异常消息外,还可以进行追溯。
不幸的是,我认为这意味着我们需要在所有pcall的下面插入错误处理程序。执行此操作的最可靠的方法似乎是这样的:
/* 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);
但这在每个pcall上都会引入噪音(我们有很多,因为我们有一些从C异步调用的Lua回调),并且感觉有些重复。我以为可以将其包装在lua_mypcall
自动执行此设置的某些函数中,但是我有两个问题:
在调用之前(或内部),这种方法是否会因更复杂的堆栈操作而中断?(我还不太熟悉Lua堆栈)
由于我们希望对大多数pcall进行追溯,因此有必要将此错误处理程序设置为默认值,并手动指定先前的错误处理程序(不执行任何操作),因此有一种方法可以全局更改默认错误Lua状态的处理程序?
我看到其中lua_pcallk
包含一些代码errfunc == 0
,但似乎不可配置。我们可以修改Lua实现以手动更改默认值,但要避免这种情况。
我们正在使用Lua 5.3。谢谢。
您的基本方法是合理的,但缺少lua_remove
(而不是lua_pop
),并且堆栈索引错误。试试这个:
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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句