trampoline.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_TRAMPOLINE_HPP
  19. #define SOL_TRAMPOLINE_HPP
  20. #include <sol/types.hpp>
  21. #include <sol/traits.hpp>
  22. #include <exception>
  23. #include <cstring>
  24. #if SOL_IS_ON(SOL_PRINT_ERRORS)
  25. #include <iostream>
  26. #endif
  27. namespace sol {
  28. // must push a single object to be the error object
  29. // NOTE: the VAST MAJORITY of all Lua libraries -- C or otherwise -- expect a string for the type of error
  30. // break this convention at your own risk
  31. using exception_handler_function = int (*)(lua_State*, optional<const std::exception&>, string_view);
  32. namespace detail {
  33. inline const char (&default_exception_handler_name())[11] {
  34. static const char name[11] = "sol.\xE2\x98\xA2\xE2\x98\xA2";
  35. return name;
  36. }
  37. // must push at least 1 object on the stack
  38. inline int default_exception_handler(lua_State* L, optional<const std::exception&>, string_view what) {
  39. #if SOL_IS_ON(SOL_PRINT_ERRORS)
  40. std::cerr << "[sol2] An exception occurred: ";
  41. std::cerr.write(what.data(), static_cast<std::streamsize>(what.size()));
  42. std::cerr << std::endl;
  43. #endif
  44. lua_pushlstring(L, what.data(), what.size());
  45. return 1;
  46. }
  47. inline int call_exception_handler(lua_State* L, optional<const std::exception&> maybe_ex, string_view what) {
  48. lua_getglobal(L, default_exception_handler_name());
  49. type t = static_cast<type>(lua_type(L, -1));
  50. if (t != type::lightuserdata) {
  51. lua_pop(L, 1);
  52. return default_exception_handler(L, std::move(maybe_ex), std::move(what));
  53. }
  54. void* vfunc = lua_touserdata(L, -1);
  55. lua_pop(L, 1);
  56. if (vfunc == nullptr) {
  57. return default_exception_handler(L, std::move(maybe_ex), std::move(what));
  58. }
  59. exception_handler_function exfunc = reinterpret_cast<exception_handler_function>(vfunc);
  60. return exfunc(L, std::move(maybe_ex), std::move(what));
  61. }
  62. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  63. template <lua_CFunction f>
  64. int static_trampoline(lua_State* L) noexcept {
  65. return f(L);
  66. }
  67. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
  68. template <lua_CFunction_noexcept f>
  69. int static_trampoline_noexcept(lua_State* L) noexcept {
  70. return f(L);
  71. }
  72. #else
  73. template <lua_CFunction f>
  74. int static_trampoline_noexcept(lua_State* L) noexcept {
  75. return f(L);
  76. }
  77. #endif
  78. template <typename Fx, typename... Args>
  79. int trampoline(lua_State* L, Fx&& f, Args&&... args) noexcept {
  80. return f(L, std::forward<Args>(args)...);
  81. }
  82. inline int c_trampoline(lua_State* L, lua_CFunction f) noexcept {
  83. return trampoline(L, f);
  84. }
  85. #else
  86. inline int lua_cfunction_trampoline(lua_State* L, lua_CFunction f) {
  87. #if SOL_IS_ON(SOL_PROPAGATE_EXCEPTIONS)
  88. return f(L);
  89. #else
  90. try {
  91. return f(L);
  92. }
  93. catch (const char* cs) {
  94. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
  95. }
  96. catch (const std::string& s) {
  97. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
  98. }
  99. catch (const std::exception& e) {
  100. call_exception_handler(L, optional<const std::exception&>(e), e.what());
  101. }
  102. #if SOL_IS_ON(SOL_EXCEPTIONS_CATCH_ALL)
  103. // LuaJIT cannot have the catchall when the safe propagation is on
  104. // but LuaJIT will swallow all C++ errors
  105. // if we don't at least catch std::exception ones
  106. catch (...) {
  107. call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
  108. }
  109. #endif // LuaJIT cannot have the catchall, but we must catch std::exceps for it
  110. return lua_error(L);
  111. #endif // Safe exceptions
  112. }
  113. template <lua_CFunction f>
  114. int static_trampoline(lua_State* L) {
  115. return lua_cfunction_trampoline(L, f);
  116. }
  117. #if SOL_IS_ON(SOL_USE_NOEXCEPT_FUNCTION_TYPE)
  118. template <lua_CFunction_noexcept f>
  119. int static_trampoline_noexcept(lua_State* L) noexcept {
  120. return f(L);
  121. }
  122. #else
  123. template <lua_CFunction f>
  124. int static_trampoline_noexcept(lua_State* L) noexcept {
  125. return f(L);
  126. }
  127. #endif
  128. template <typename Fx, typename... Args>
  129. int trampoline(lua_State* L, Fx&& f, Args&&... args) {
  130. if constexpr (meta::bind_traits<meta::unqualified_t<Fx>>::is_noexcept) {
  131. return f(L, std::forward<Args>(args)...);
  132. }
  133. else {
  134. #if SOL_IS_ON(SOL_PROPAGATE_EXCEPTIONS)
  135. return f(L, std::forward<Args>(args)...);
  136. #else
  137. try {
  138. return f(L, std::forward<Args>(args)...);
  139. }
  140. catch (const char* cs) {
  141. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(cs));
  142. }
  143. catch (const std::string& s) {
  144. call_exception_handler(L, optional<const std::exception&>(nullopt), string_view(s.c_str(), s.size()));
  145. }
  146. catch (const std::exception& e) {
  147. call_exception_handler(L, optional<const std::exception&>(e), e.what());
  148. }
  149. #if SOL_IS_ON(SOL_EXCEPTIONS_CATCH_ALL)
  150. // LuaJIT cannot have the catchall when the safe propagation is on
  151. // but LuaJIT will swallow all C++ errors
  152. // if we don't at least catch std::exception ones
  153. catch (...) {
  154. call_exception_handler(L, optional<const std::exception&>(nullopt), "caught (...) exception");
  155. }
  156. #endif
  157. return lua_error(L);
  158. #endif
  159. }
  160. }
  161. inline int c_trampoline(lua_State* L, lua_CFunction f) {
  162. return trampoline(L, f);
  163. }
  164. #endif // Exceptions vs. No Exceptions
  165. template <typename F, F fx>
  166. inline int typed_static_trampoline(lua_State* L) {
  167. #if 0
  168. // TODO: you must evaluate the get/check_get of every
  169. // argument, to ensure it doesn't throw
  170. // (e.g., for the sol_lua_check_access extension point!)
  171. // This incluudes properly noexcept-ing all the above
  172. // trampolines / safety nets
  173. if constexpr (meta::bind_traits<F>::is_noexcept) {
  174. return static_trampoline_noexcept<fx>(L);
  175. }
  176. else
  177. #endif
  178. { return static_trampoline<fx>(L); }
  179. }
  180. } // namespace detail
  181. inline void set_default_exception_handler(lua_State* L, exception_handler_function exf = &detail::default_exception_handler) {
  182. static_assert(sizeof(void*) >= sizeof(exception_handler_function),
  183. "void* storage is too small to transport the exception handler: please file a bug on the sol2 issue tracker to get this looked at!");
  184. void* storage;
  185. std::memcpy(&storage, &exf, sizeof(exception_handler_function));
  186. lua_pushlightuserdata(L, storage);
  187. lua_setglobal(L, detail::default_exception_handler_name());
  188. }
  189. } // namespace sol
  190. #endif // SOL_TRAMPOLINE_HPP