summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/sol2/sol/state.hpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/sol2/sol/state.hpp')
-rw-r--r--3rdparty/sol2/sol/state.hpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/3rdparty/sol2/sol/state.hpp b/3rdparty/sol2/sol/state.hpp
index b2b03622748..97bbc260dc6 100644
--- a/3rdparty/sol2/sol/state.hpp
+++ b/3rdparty/sol2/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<string_detail::string_shim> maybetopmsg = stack::check_get<string_detail::string_shim>(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<string_detail::string_shim> maybetraceback = stack::check_get<string_detail::string_shim>(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<lua_State, void(*)(lua_State*)>, public state_view {
private:
typedef std::unique_ptr<lua_State, void(*)(lua_State*)> unique_base;
@@ -53,10 +71,23 @@ 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());
}
+ state(const state&) = delete;
+ state(state&&) = default;
+ state& operator=(const state&) = delete;
+ state& operator=(state&&) = default;
+
using state_view::get;
+
+ ~state() {
+ auto& handler = protected_function::get_default_handler();
+ if (handler.lua_state() == this->lua_state()) {
+ protected_function::set_default_handler(reference());
+ }
+ }
};
} // sol