From b432407dcef0743023985c459cf5e1702300f6dd Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 8 Dec 2023 09:11:36 +0100 Subject: [PATCH] fix(lua): add default handler to every protected function calls, until now there was none/scuffed and nothing was getting output to the user making debugging very difficult (#2494) --- src/lua/lua_module.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lua/lua_module.cpp b/src/lua/lua_module.cpp index 97a62824..2cfb4418 100644 --- a/src/lua/lua_module.cpp +++ b/src/lua/lua_module.cpp @@ -60,6 +60,26 @@ namespace big // When this function exits, Lua will exhibit default behavior and abort() } + static int traceback_error_handler(lua_State* L) + { + std::string msg = "An unknown error has triggered the error handler"; + sol::optional maybetopmsg = sol::stack::unqualified_check_get(L, 1, &sol::no_panic); + if (maybetopmsg) + { + const sol::string_view& topmsg = maybetopmsg.value(); + msg.assign(topmsg.data(), topmsg.size()); + } + luaL_traceback(L, L, msg.c_str(), 1); + sol::optional maybetraceback = sol::stack::unqualified_check_get(L, -1, &sol::no_panic); + if (maybetraceback) + { + const sol::string_view& traceback = maybetraceback.value(); + msg.assign(traceback.data(), traceback.size()); + } + LOG(FATAL) << msg; + return sol::stack::push(L, msg); + } + lua_module::lua_module(const std::filesystem::path& module_path, folder& scripts_folder) : m_state(), m_module_path(module_path), @@ -87,6 +107,8 @@ namespace big m_state.set_exception_handler(exception_handler); m_state.set_panic(sol::c_call); + lua_CFunction traceback_function = sol::c_call; + sol::protected_function::set_default_handler(sol::object(m_state.lua_state(), sol::in_place, traceback_function)); m_last_write_time = std::filesystem::last_write_time(m_module_path); }