1 // The MIT License (MIT) 2 3 // Copyright (c) 2013-2016 Rapptz, ThePhD and contributors 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy of 6 // this software and associated documentation files (the "Software"), to deal in 7 // the Software without restriction, including without limitation the rights to 8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 // the Software, and to permit persons to whom the Software is furnished to do so, 10 // subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22 #ifndef SOL_STATE_HPP 23 #define SOL_STATE_HPP 24 25 #include "state_view.hpp" 26 27 namespace sol { default_at_panic(lua_State * L)28 inline int default_at_panic(lua_State* L) { 29 #ifdef SOL_NO_EXCEPTIONS 30 (void)L; 31 return -1; 32 #else 33 const char* message = lua_tostring(L, -1); 34 if (message) { 35 std::string err = message; 36 lua_pop(L, 1); 37 throw error(err); 38 } 39 throw error(std::string("An unexpected error occurred and forced the lua state to call atpanic")); 40 #endif 41 } 42 default_error_handler(lua_State * L)43 inline int default_error_handler(lua_State*L) { 44 using namespace sol; 45 std::string msg = "An unknown error has triggered the default error handler"; 46 optional<string_detail::string_shim> maybetopmsg = stack::check_get<string_detail::string_shim>(L, 1); 47 if (maybetopmsg) { 48 const string_detail::string_shim& topmsg = maybetopmsg.value(); 49 msg.assign(topmsg.c_str(), topmsg.size()); 50 } 51 luaL_traceback(L, L, msg.c_str(), 1); 52 optional<string_detail::string_shim> maybetraceback = stack::check_get<string_detail::string_shim>(L, -1); 53 if (maybetraceback) { 54 const string_detail::string_shim& traceback = maybetraceback.value(); 55 msg.assign(traceback.c_str(), traceback.size()); 56 } 57 return stack::push(L, msg); 58 } 59 60 61 class state : private std::unique_ptr<lua_State, void(*)(lua_State*)>, public state_view { 62 private: 63 typedef std::unique_ptr<lua_State, void(*)(lua_State*)> unique_base; 64 public: state(lua_CFunction panic=default_at_panic)65 state(lua_CFunction panic = default_at_panic) : unique_base(luaL_newstate(), lua_close), 66 state_view(unique_base::get()) { 67 set_panic(panic); 68 stack::luajit_exception_handler(unique_base::get()); 69 } 70 state(lua_CFunction panic,lua_Alloc alfunc,void * alpointer=nullptr)71 state(lua_CFunction panic, lua_Alloc alfunc, void* alpointer = nullptr) : unique_base(lua_newstate(alfunc, alpointer), lua_close), 72 state_view(unique_base::get()) { 73 set_panic(panic); 74 sol::protected_function::set_default_handler(sol::object(lua_state(), in_place, default_error_handler)); 75 stack::luajit_exception_handler(unique_base::get()); 76 } 77 78 state(const state&) = delete; 79 state(state&&) = default; 80 state& operator=(const state&) = delete; 81 state& operator=(state&&) = default; 82 83 using state_view::get; 84 ~state()85 ~state() { 86 auto& handler = protected_function::get_default_handler(); 87 if (handler.lua_state() == this->lua_state()) { 88 protected_function::set_default_handler(reference()); 89 } 90 } 91 }; 92 } // sol 93 94 #endif // SOL_STATE_HPP 95