我想debug.traceback()
在c++中调用lua函数来获取c++中的trackback信息,所以我在c++中添加了这样的函数:
int luaErrorHandler(lua_State *m_state) {
if (!lua_isstring(m_state, 1))
return 1;
lua_getfield(m_state, LUA_GLOBALSINDEX, "debug");
if (!lua_istable(m_state, -1)) {
lua_pop(m_state, 1);
return 1;
}
lua_getfield(m_state, -1, "traceback");
if (!lua_isfunction(m_state, -1)) {
lua_pop(m_state, 2);
return 1;
}
lua_pushvalue(m_state, 1); /* pass error message */
lua_pushinteger(m_state, 2);
lua_call(m_state, 2, 1); /* call debug.traceback */
return 1;
}
然后我lua_pushcfunction(L, luaErrorHandler)
把它压入堆栈,然后lua_insert()
把函数移到底部,然后lua_pcall(L, nArgs, 1, errIndex)
调用lua函数。此时的栈应该是这样的: ..luaErrorHandler , func, arg1 , arg2....
问题是当我调用函数时,函数以某种方式改变了自身内部的堆栈(我认为..),所以我收到错误“尝试调用数字值”,当我不使用错误处理程序时它会正确运行功能。有没有关于如何debug.traceback()
正确使用的建议?或者我该如何调试这个问题,因为我完全不知道它是怎么出错的。