我要調用的LUA功能debug.traceback()
在C++中獲得在C++中引用的信息,所以我加了C++中的功能是這樣的:LUA debug.traceback
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()
的建議?或者我該如何調試這個問題,因爲我完全不知道它是怎麼出錯的。