state_handling.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_STATE_DEFAULT_HPP
  19. #define SOL_STATE_DEFAULT_HPP
  20. #include <sol/trampoline.hpp>
  21. #include <sol/stack.hpp>
  22. #include <sol/error.hpp>
  23. #include <sol/function.hpp>
  24. #include <sol/object.hpp>
  25. #include <sol/lua_value.hpp>
  26. #if SOL_IS_ON(SOL_PRINT_ERRORS)
  27. #include <iostream>
  28. #endif
  29. namespace sol {
  30. inline void register_main_thread(lua_State* L) {
  31. #if SOL_LUA_VERSION_I_ < 502
  32. if (L == nullptr) {
  33. lua_pushnil(L);
  34. lua_setglobal(L, detail::default_main_thread_name());
  35. return;
  36. }
  37. lua_pushthread(L);
  38. lua_setglobal(L, detail::default_main_thread_name());
  39. #else
  40. (void)L;
  41. #endif
  42. }
  43. inline int default_at_panic(lua_State* L) {
  44. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  45. (void)L;
  46. return -1;
  47. #else
  48. size_t messagesize;
  49. const char* message = lua_tolstring(L, -1, &messagesize);
  50. if (message) {
  51. std::string err(message, messagesize);
  52. lua_settop(L, 0);
  53. #if SOL_IS_ON(SOL_PRINT_ERRORS)
  54. std::cerr << "[sol2] An error occurred and panic has been invoked: ";
  55. std::cerr << err;
  56. std::cerr << std::endl;
  57. #endif
  58. throw error(err);
  59. }
  60. lua_settop(L, 0);
  61. throw error(std::string("An unexpected error occurred and panic has been invoked"));
  62. #endif // Printing Errors
  63. }
  64. inline int default_traceback_error_handler(lua_State* L) {
  65. std::string msg = "An unknown error has triggered the default error handler";
  66. optional<string_view> maybetopmsg = stack::unqualified_check_get<string_view>(L, 1, &no_panic);
  67. if (maybetopmsg) {
  68. const string_view& topmsg = maybetopmsg.value();
  69. msg.assign(topmsg.data(), topmsg.size());
  70. }
  71. luaL_traceback(L, L, msg.c_str(), 1);
  72. optional<string_view> maybetraceback = stack::unqualified_check_get<string_view>(L, -1, &no_panic);
  73. if (maybetraceback) {
  74. const string_view& traceback = maybetraceback.value();
  75. msg.assign(traceback.data(), traceback.size());
  76. }
  77. #if SOL_IS_ON(SOL_PRINT_ERRORS)
  78. // std::cerr << "[sol2] An error occurred and was caught in traceback: ";
  79. // std::cerr << msg;
  80. // std::cerr << std::endl;
  81. #endif // Printing
  82. return stack::push(L, msg);
  83. }
  84. inline void set_default_state(lua_State* L, lua_CFunction panic_function = &default_at_panic,
  85. lua_CFunction traceback_function = c_call<decltype(&default_traceback_error_handler), &default_traceback_error_handler>,
  86. exception_handler_function exf = detail::default_exception_handler) {
  87. lua_atpanic(L, panic_function);
  88. protected_function::set_default_handler(object(L, in_place, traceback_function));
  89. set_default_exception_handler(L, exf);
  90. register_main_thread(L);
  91. stack::luajit_exception_handler(L);
  92. lua_value::set_lua_state(L);
  93. }
  94. inline std::size_t total_memory_used(lua_State* L) {
  95. std::size_t kb = static_cast<std::size_t>(lua_gc(L, LUA_GCCOUNT, 0));
  96. kb *= 1024;
  97. kb += static_cast<std::size_t>(lua_gc(L, LUA_GCCOUNTB, 0));
  98. return kb;
  99. }
  100. inline protected_function_result script_pass_on_error(lua_State*, protected_function_result result) {
  101. return result;
  102. }
  103. inline protected_function_result script_throw_on_error(lua_State* L, protected_function_result result) {
  104. type t = type_of(L, result.stack_index());
  105. std::string err = "sol: ";
  106. err += to_string(result.status());
  107. err += " error";
  108. #if SOL_IS_ON(SOL_EXCEPTIONS)
  109. std::exception_ptr eptr = std::current_exception();
  110. if (eptr) {
  111. err += " with a ";
  112. try {
  113. std::rethrow_exception(eptr);
  114. }
  115. catch (const std::exception& ex) {
  116. err += "std::exception -- ";
  117. err.append(ex.what());
  118. }
  119. catch (const std::string& message) {
  120. err += "thrown message -- ";
  121. err.append(message);
  122. }
  123. catch (const char* message) {
  124. err += "thrown message -- ";
  125. err.append(message);
  126. }
  127. catch (...) {
  128. err.append("thrown but unknown type, cannot serialize into error message");
  129. }
  130. }
  131. #endif // serialize exception information if possible
  132. if (t == type::string) {
  133. err += ": ";
  134. string_view serr = stack::unqualified_get<string_view>(L, result.stack_index());
  135. err.append(serr.data(), serr.size());
  136. }
  137. #if SOL_IS_ON(SOL_PRINT_ERRORS)
  138. std::cerr << "[sol2] An error occurred and has been passed to an error handler: ";
  139. std::cerr << err;
  140. std::cerr << std::endl;
  141. #endif
  142. // replacing information of stack error into pfr
  143. int target = result.stack_index();
  144. if (result.pop_count() > 0) {
  145. stack::remove(L, target, result.pop_count());
  146. }
  147. stack::push(L, err);
  148. int top = lua_gettop(L);
  149. int towards = top - target;
  150. if (towards != 0) {
  151. lua_rotate(L, top, towards);
  152. }
  153. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  154. return result;
  155. #else
  156. // just throw our error
  157. throw error(detail::direct_error, err);
  158. #endif // If exceptions are allowed
  159. }
  160. inline protected_function_result script_default_on_error(lua_State* L, protected_function_result pfr) {
  161. #if SOL_IS_ON(SOL_DEFAULT_PASS_ON_ERROR)
  162. return script_pass_on_error(L, std::move(pfr));
  163. #else
  164. return script_throw_on_error(L, std::move(pfr));
  165. #endif
  166. }
  167. namespace stack {
  168. inline error get_traceback_or_errors(lua_State* L) {
  169. int p = default_traceback_error_handler(L);
  170. sol::error err = stack::get<sol::error>(L, -p);
  171. lua_pop(L, p);
  172. return err;
  173. }
  174. } // namespace stack
  175. } // namespace sol
  176. #endif // SOL_STATE_DEFAULT_HPP