error_handler.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_ERROR_HANDLER_HPP
  19. #define SOL_ERROR_HANDLER_HPP
  20. #include <sol/types.hpp>
  21. #include <sol/demangle.hpp>
  22. #include <cstdio>
  23. namespace sol {
  24. namespace detail {
  25. constexpr const char* not_a_number = "not a numeric type";
  26. constexpr const char* not_a_number_or_number_string = "not a numeric type or numeric string";
  27. constexpr const char* not_a_number_integral = "not a numeric type that fits exactly an integer (number maybe has significant decimals)";
  28. constexpr const char* not_a_number_or_number_string_integral
  29. = "not a numeric type or a numeric string that fits exactly an integer (e.g. number maybe has significant decimals)";
  30. constexpr const char* not_enough_stack_space = "not enough space left on Lua stack";
  31. constexpr const char* not_enough_stack_space_floating = "not enough space left on Lua stack for a floating point number";
  32. constexpr const char* not_enough_stack_space_integral = "not enough space left on Lua stack for an integral number";
  33. constexpr const char* not_enough_stack_space_string = "not enough space left on Lua stack for a string";
  34. constexpr const char* not_enough_stack_space_meta_function_name = "not enough space left on Lua stack for the name of a meta_function";
  35. constexpr const char* not_enough_stack_space_userdata = "not enough space left on Lua stack to create a sol2 userdata";
  36. constexpr const char* not_enough_stack_space_generic = "not enough space left on Lua stack to push valuees";
  37. constexpr const char* not_enough_stack_space_environment = "not enough space left on Lua stack to retrieve environment";
  38. constexpr const char* protected_function_error = "caught (...) unknown error during protected_function call";
  39. inline void accumulate_and_mark(const std::string& n, std::string& aux_message, int& marker) {
  40. if (marker > 0) {
  41. aux_message += ", ";
  42. }
  43. aux_message += n;
  44. ++marker;
  45. }
  46. } // namespace detail
  47. inline std::string associated_type_name(lua_State* L, int index, type t) {
  48. switch (t) {
  49. case type::poly:
  50. return "anything";
  51. case type::userdata: {
  52. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  53. luaL_checkstack(L, 2, "not enough space to push get the type name");
  54. #endif // make sure stack doesn't overflow
  55. if (lua_getmetatable(L, index) == 0) {
  56. break;
  57. }
  58. lua_pushlstring(L, "__name", 6);
  59. lua_rawget(L, -2);
  60. size_t sz;
  61. const char* name = lua_tolstring(L, -1, &sz);
  62. std::string tn(name, static_cast<std::string::size_type>(sz));
  63. lua_pop(L, 2);
  64. return tn;
  65. }
  66. default:
  67. break;
  68. }
  69. return lua_typename(L, static_cast<int>(t));
  70. }
  71. inline int push_type_panic_string(lua_State* L, int index, type expected, type actual, string_view message, string_view aux_message) noexcept {
  72. const char* err = message.size() == 0
  73. ? (aux_message.size() == 0 ? "stack index %d, expected %s, received %s" : "stack index %d, expected %s, received %s: %s%s")
  74. : "stack index %d, expected %s, received %s: %s %s";
  75. const char* type_name = expected == type::poly ? "anything" : lua_typename(L, static_cast<int>(expected));
  76. {
  77. std::string actual_name = associated_type_name(L, index, actual);
  78. lua_pushfstring(L, err, index, type_name, actual_name.c_str(), message.data(), aux_message.data());
  79. }
  80. return 1;
  81. }
  82. inline int type_panic_string(lua_State* L, int index, type expected, type actual, string_view message = "") noexcept(false) {
  83. push_type_panic_string(L, index, expected, actual, message, "");
  84. return lua_error(L);
  85. }
  86. inline int type_panic_c_str(lua_State* L, int index, type expected, type actual, const char* message = nullptr) noexcept(false) {
  87. push_type_panic_string(L, index, expected, actual, message == nullptr ? "" : message, "");
  88. return lua_error(L);
  89. }
  90. struct type_panic_t {
  91. int operator()(lua_State* L, int index, type expected, type actual) const noexcept(false) {
  92. return type_panic_c_str(L, index, expected, actual, nullptr);
  93. }
  94. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  95. return type_panic_c_str(L, index, expected, actual, message.data());
  96. }
  97. };
  98. const type_panic_t type_panic = {};
  99. struct constructor_handler {
  100. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  101. push_type_panic_string(L, index, expected, actual, message, "(type check failed in constructor)");
  102. return lua_error(L);
  103. }
  104. };
  105. template <typename F = void>
  106. struct argument_handler {
  107. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  108. push_type_panic_string(L, index, expected, actual, message, "(bad argument to variable or function call)");
  109. return lua_error(L);
  110. }
  111. };
  112. template <typename R, typename... Args>
  113. struct argument_handler<types<R, Args...>> {
  114. int operator()(lua_State* L, int index, type expected, type actual, string_view message) const noexcept(false) {
  115. {
  116. std::string aux_message = "(bad argument into '";
  117. aux_message += detail::demangle<R>();
  118. aux_message += "(";
  119. int marker = 0;
  120. (void)detail::swallow { int(), (detail::accumulate_and_mark(detail::demangle<Args>(), aux_message, marker), int())... };
  121. aux_message += ")')";
  122. push_type_panic_string(L, index, expected, actual, message, aux_message);
  123. }
  124. return lua_error(L);
  125. }
  126. };
  127. // Specify this function as the handler for lua::check if you know there's nothing wrong
  128. inline int no_panic(lua_State*, int, type, type, const char* = nullptr) noexcept {
  129. return 0;
  130. }
  131. inline void type_error(lua_State* L, int expected, int actual) noexcept(false) {
  132. luaL_error(L, "expected %s, received %s", lua_typename(L, expected), lua_typename(L, actual));
  133. }
  134. inline void type_error(lua_State* L, type expected, type actual) noexcept(false) {
  135. type_error(L, static_cast<int>(expected), static_cast<int>(actual));
  136. }
  137. inline void type_assert(lua_State* L, int index, type expected, type actual) noexcept(false) {
  138. if (expected != type::poly && expected != actual) {
  139. type_panic_c_str(L, index, expected, actual, nullptr);
  140. }
  141. }
  142. inline void type_assert(lua_State* L, int index, type expected) {
  143. type actual = type_of(L, index);
  144. type_assert(L, index, expected, actual);
  145. }
  146. } // namespace sol
  147. #endif // SOL_ERROR_HANDLER_HPP