From 371b39a09a0bf248d674fae718f5ff369e895b66 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Tue, 5 Nov 2013 12:06:15 -0500 Subject: Pass a errfunc to lua_pcall to get a traceback --- src/script/cpp_api/s_mainmenu.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/script/cpp_api/s_mainmenu.cpp') diff --git a/src/script/cpp_api/s_mainmenu.cpp b/src/script/cpp_api/s_mainmenu.cpp index af92c59a9..5c54f7368 100644 --- a/src/script/cpp_api/s_mainmenu.cpp +++ b/src/script/cpp_api/s_mainmenu.cpp @@ -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 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 } // 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 } -- cgit v1.2.3