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)

This commit is contained in:
Quentin 2023-12-08 09:11:36 +01:00 committed by GitHub
parent 95b9724fff
commit b432407dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -60,6 +60,26 @@ namespace big
// When this function exits, Lua will exhibit default behavior and abort() // 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<sol::string_view> maybetopmsg = sol::stack::unqualified_check_get<sol::string_view>(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<sol::string_view> maybetraceback = sol::stack::unqualified_check_get<sol::string_view>(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) : lua_module::lua_module(const std::filesystem::path& module_path, folder& scripts_folder) :
m_state(), m_state(),
m_module_path(module_path), m_module_path(module_path),
@ -87,6 +107,8 @@ namespace big
m_state.set_exception_handler(exception_handler); m_state.set_exception_handler(exception_handler);
m_state.set_panic(sol::c_call<decltype(&panic_handler), &panic_handler>); m_state.set_panic(sol::c_call<decltype(&panic_handler), &panic_handler>);
lua_CFunction traceback_function = sol::c_call<decltype(&traceback_error_handler), &traceback_error_handler>;
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); m_last_write_time = std::filesystem::last_write_time(m_module_path);
} }