unsafe_function.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_UNSAFE_FUNCTION_HPP
  19. #define SOL_UNSAFE_FUNCTION_HPP
  20. #include <sol/reference.hpp>
  21. #include <sol/object.hpp>
  22. #include <sol/stack.hpp>
  23. #include <sol/function_result.hpp>
  24. #include <sol/function_types.hpp>
  25. #include <sol/bytecode.hpp>
  26. #include <sol/dump_handler.hpp>
  27. #include <cstdint>
  28. namespace sol {
  29. template <typename ref_t, bool aligned = false>
  30. class basic_function : public basic_object<ref_t> {
  31. private:
  32. using base_t = basic_object<ref_t>;
  33. void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const {
  34. lua_call(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount));
  35. }
  36. template <std::size_t... I, typename... Ret>
  37. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n) const {
  38. luacall(n, lua_size<std::tuple<Ret...>>::value);
  39. return stack::pop<std::tuple<Ret...>>(lua_state());
  40. }
  41. template <std::size_t I, typename Ret, meta::enable<meta::neg<std::is_void<Ret>>> = meta::enabler>
  42. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n) const {
  43. luacall(n, lua_size<Ret>::value);
  44. return stack::pop<Ret>(lua_state());
  45. }
  46. template <std::size_t I>
  47. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n) const {
  48. luacall(n, 0);
  49. }
  50. unsafe_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) const {
  51. int stacksize = lua_gettop(lua_state());
  52. int firstreturn = (std::max)(1, stacksize - static_cast<int>(n));
  53. luacall(n, LUA_MULTRET);
  54. int poststacksize = lua_gettop(lua_state());
  55. int returncount = poststacksize - (firstreturn - 1);
  56. return unsafe_function_result(lua_state(), firstreturn, returncount);
  57. }
  58. public:
  59. using base_t::lua_state;
  60. basic_function() = default;
  61. template <typename T,
  62. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_function>>, meta::neg<std::is_same<base_t, stack_reference>>,
  63. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  64. basic_function(T&& r) noexcept : base_t(std::forward<T>(r)) {
  65. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  66. if (!is_function<meta::unqualified_t<T>>::value) {
  67. auto pp = stack::push_pop(*this);
  68. constructor_handler handler {};
  69. stack::check<basic_function>(lua_state(), -1, handler);
  70. }
  71. #endif // Safety
  72. }
  73. basic_function(const basic_function&) = default;
  74. basic_function& operator=(const basic_function&) = default;
  75. basic_function(basic_function&&) = default;
  76. basic_function& operator=(basic_function&&) = default;
  77. basic_function(const stack_reference& r) : basic_function(r.lua_state(), r.stack_index()) {
  78. }
  79. basic_function(stack_reference&& r) : basic_function(r.lua_state(), r.stack_index()) {
  80. }
  81. basic_function(lua_nil_t n) : base_t(n) {
  82. }
  83. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  84. basic_function(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
  85. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  86. auto pp = stack::push_pop(*this);
  87. constructor_handler handler {};
  88. stack::check<basic_function>(lua_state(), -1, handler);
  89. #endif // Safety
  90. }
  91. basic_function(lua_State* L, int index = -1) : base_t(L, index) {
  92. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  93. constructor_handler handler {};
  94. stack::check<basic_function>(L, index, handler);
  95. #endif // Safety
  96. }
  97. basic_function(lua_State* L, ref_index index) : base_t(L, index) {
  98. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  99. auto pp = stack::push_pop(*this);
  100. constructor_handler handler {};
  101. stack::check<basic_function>(lua_state(), -1, handler);
  102. #endif // Safety
  103. }
  104. template <typename Fx>
  105. int dump(lua_Writer writer, void* userdata, bool strip, Fx&& on_error) const {
  106. this->push();
  107. auto ppn = stack::push_popper_n<false>(this->lua_state(), 1);
  108. int r = lua_dump(this->lua_state(), writer, userdata, strip ? 1 : 0);
  109. if (r != 0) {
  110. return on_error(this->lua_state(), r, writer, userdata, strip);
  111. }
  112. return r;
  113. }
  114. int dump(lua_Writer writer, void* userdata, bool strip = false) const {
  115. return dump(writer, userdata, strip, &dump_throw_on_error);
  116. }
  117. template <typename Container = bytecode>
  118. Container dump() const {
  119. Container bc;
  120. (void)dump(static_cast<lua_Writer>(&basic_insert_dump_writer<Container>), static_cast<void*>(&bc), false, &dump_panic_on_error);
  121. return bc;
  122. }
  123. template <typename Container = bytecode, typename Fx>
  124. Container dump(Fx&& on_error) const {
  125. Container bc;
  126. (void)dump(static_cast<lua_Writer>(&basic_insert_dump_writer<Container>), static_cast<void*>(&bc), false, std::forward<Fx>(on_error));
  127. return bc;
  128. }
  129. template <typename... Args>
  130. unsafe_function_result operator()(Args&&... args) const {
  131. return call<>(std::forward<Args>(args)...);
  132. }
  133. template <typename... Ret, typename... Args>
  134. decltype(auto) operator()(types<Ret...>, Args&&... args) const {
  135. return call<Ret...>(std::forward<Args>(args)...);
  136. }
  137. template <typename... Ret, typename... Args>
  138. decltype(auto) call(Args&&... args) const {
  139. if (!aligned) {
  140. base_t::push();
  141. }
  142. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  143. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), static_cast<std::ptrdiff_t>(pushcount));
  144. }
  145. };
  146. } // namespace sol
  147. #endif // SOL_UNSAFE_FUNCTION_HPP