1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Pass a errfunc to lua_pcall to get a traceback

This commit is contained in:
ShadowNinja 2013-11-05 12:06:15 -05:00
parent 3f519eb729
commit 371b39a09a
19 changed files with 424 additions and 324 deletions

View file

@ -37,29 +37,41 @@ void ScriptApiMainMenu::handleMainMenuEvent(std::string text)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Get handler function
lua_getglobal(L, "engine");
lua_getfield(L, -1, "event_handler");
if(lua_isnil(L, -1))
lua_remove(L, -2); // Remove engine
if(lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop event_handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
// Call it
lua_pushstring(L, text.c_str());
if(lua_pcall(L, 1, 0, 0))
scriptError("error running function engine.event_handler: %s\n",
lua_tostring(L, -1));
if(lua_pcall(L, 1, 0, errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string> fields)
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Get handler function
lua_getglobal(L, "engine");
lua_getfield(L, -1, "button_handler");
if(lua_isnil(L, -1))
lua_remove(L, -2); // Remove engine
if(lua_isnil(L, -1)) {
lua_pop(L, 1); // Pop button handler
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
// Convert fields to lua table
@ -74,7 +86,7 @@ void ScriptApiMainMenu::handleMainMenuButtons(std::map<std::string, std::string>
}
// Call it
if(lua_pcall(L, 1, 0, 0))
scriptError("error running function engine.button_handler: %s\n",
lua_tostring(L, -1));
if(lua_pcall(L, 1, 0, errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}