protected_handler.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_PROTECTED_HANDLER_HPP
  19. #define SOL_PROTECTED_HANDLER_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/stack.hpp>
  22. #include <sol/protected_function_result.hpp>
  23. #include <sol/unsafe_function.hpp>
  24. #include <cstdint>
  25. namespace sol { namespace detail {
  26. inline const char (&default_handler_name())[9] {
  27. static const char name[9] = "sol.\xF0\x9F\x94\xA9";
  28. return name;
  29. }
  30. template <bool ShouldPush, typename Target = reference>
  31. struct protected_handler {
  32. lua_State* m_L;
  33. const Target& target;
  34. int stack_index;
  35. protected_handler(std::false_type, lua_State* L_, const Target& target_) : m_L(L_), target(target_), stack_index(0) {
  36. if (ShouldPush) {
  37. stack_index = lua_gettop(L_) + 1;
  38. target.push(L_);
  39. }
  40. }
  41. protected_handler(std::true_type, lua_State* L_, const Target& target_) : m_L(L_), target(target_), stack_index(0) {
  42. if (ShouldPush) {
  43. stack_index = target.stack_index();
  44. }
  45. }
  46. protected_handler(lua_State* L_, const Target& target_) : protected_handler(meta::boolean<is_stack_based_v<Target>>(), L_, target_) {
  47. }
  48. bool valid() const noexcept {
  49. return ShouldPush;
  50. }
  51. ~protected_handler() {
  52. if constexpr (!is_stack_based_v<Target>) {
  53. if (stack_index != 0) {
  54. lua_remove(m_L, stack_index);
  55. }
  56. }
  57. }
  58. };
  59. template <typename Base, typename T>
  60. inline basic_function<Base> force_cast(T& p) {
  61. return p;
  62. }
  63. template <typename Reference, bool IsMainReference = false>
  64. inline Reference get_default_handler(lua_State* L_) {
  65. if (is_stack_based_v<Reference> || L_ == nullptr)
  66. return Reference(L_, lua_nil);
  67. L_ = IsMainReference ? main_thread(L_, L_) : L_;
  68. lua_getglobal(L_, default_handler_name());
  69. auto pp = stack::pop_n(L_, 1);
  70. return Reference(L_, -1);
  71. }
  72. template <typename T>
  73. inline void set_default_handler(lua_State* L, const T& ref) {
  74. if (L == nullptr) {
  75. return;
  76. }
  77. if (!ref.valid()) {
  78. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  79. luaL_checkstack(L, 1, detail::not_enough_stack_space_generic);
  80. #endif // make sure stack doesn't overflow
  81. lua_pushnil(L);
  82. lua_setglobal(L, default_handler_name());
  83. }
  84. else {
  85. ref.push(L);
  86. lua_setglobal(L, default_handler_name());
  87. }
  88. }
  89. }} // namespace sol::detail
  90. #endif // SOL_PROTECTED_HANDLER_HPP