From 948090676a838917f9fdc918fefc1caa403171b7 Mon Sep 17 00:00:00 2001 From: Koncord Date: Wed, 1 Nov 2017 01:17:00 +0800 Subject: [PATCH] [Server] Impove Lua Error handler for Windows --- apps/openmw-mp/Script/LuaState.cpp | 32 ++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/apps/openmw-mp/Script/LuaState.cpp b/apps/openmw-mp/Script/LuaState.cpp index 6deee4431..c49aeb2dc 100644 --- a/apps/openmw-mp/Script/LuaState.cpp +++ b/apps/openmw-mp/Script/LuaState.cpp @@ -31,6 +31,30 @@ using namespace std; + +#if defined(SOL_SAFE_FUNCTIONS) && defined(WIN32) +inline int errHandler(lua_State *L) +{ + string msg = "An unknown error has triggered the default error handler"; + auto maybetopmsg = sol::stack::check_get(L, 1); + if (maybetopmsg) + { + const sol::string_view &topmsg = maybetopmsg.value(); + msg.assign(topmsg.data(), topmsg.size()); + } + luaL_tramceback(L, L, msg.c_str(), 1); + auto maybetraceback = sol::stack::check_get(L, -1); + if (maybetraceback) + { + const sol::string_view &traceback = maybetraceback.value(); + msg.assign(traceback.data(), traceback.size()); + } + LOG_MESSAGE_SIMPLE(Log::LOG_ERROR, msg.c_str()); + abort(); + return 0; // Not used because the server is already terminated +} +#endif + LuaState::LuaState() { lua = make_shared(); @@ -83,12 +107,8 @@ LuaState::LuaState() // Enable a special Sol error handler for Windows, because exceptions aren't caught properly // in main.cpp for it #if defined(SOL_SAFE_FUNCTIONS) && defined(WIN32) - lua->set_function("ErrorHandler", [](sol::object error_msg) { - LOG_MESSAGE_SIMPLE(Log::LOG_ERROR, ("Lua: " + error_msg.as()).c_str()); - }); - - sol::reference errHandler = (*lua)["ErrorHandler"]; - sol::protected_function::set_default_handler(errHandler); + lua_CFunction f = sol::c_call; + sol::protected_function::set_default_handler(sol::object(lua->lua_state(), sol::in_place, f)); #endif sol::table Constants = lua->create_named_table("Constants");