From 97cafbaadd914350fccb2540519b74727eba9c17 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Thu, 17 Nov 2016 23:58:24 -0500 Subject: [PATCH] Default error handler function for protected_function --- sol/state.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sol/state.hpp b/sol/state.hpp index 4f127844..1cdb65d1 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -40,6 +40,24 @@ namespace sol { #endif } + inline int default_error_handler(lua_State*L) { + using namespace sol; + std::string msg = "An unknown error has triggered the default error handler"; + optional maybetopmsg = stack::check_get(L, 1); + if (maybetopmsg) { + const string_detail::string_shim& topmsg = maybetopmsg.value(); + msg.assign(topmsg.c_str(), topmsg.size()); + } + luaL_traceback(L, L, msg.c_str(), 1); + optional maybetraceback = stack::check_get(L, -1); + if (maybetraceback) { + const string_detail::string_shim& traceback = maybetraceback.value(); + msg.assign(traceback.c_str(), traceback.size()); + } + return stack::push(L, msg); + } + + class state : private std::unique_ptr, public state_view { private: typedef std::unique_ptr unique_base; @@ -53,6 +71,7 @@ namespace sol { state(lua_CFunction panic, lua_Alloc alfunc, void* alpointer = nullptr) : unique_base(lua_newstate(alfunc, alpointer), lua_close), state_view(unique_base::get()) { set_panic(panic); + sol::protected_function::set_default_handler(sol::object(lua_state(), in_place, default_error_handler)); stack::luajit_exception_handler(unique_base::get()); }