packaged_coroutine.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_PACKAGED_COROUTINE_HPP
  19. #define SOL_PACKAGED_COROUTINE_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/thread.hpp>
  25. #include <sol/protected_handler.hpp>
  26. #include <sol/coroutine.hpp>
  27. namespace sol {
  28. #if 0
  29. class packaged_coroutine {
  30. private:
  31. lua_State* m_L;
  32. sol::stateless_reference m_coroutine_reference;
  33. sol::stateless_reference m_error_handler;
  34. sol::thread m_thread_reference;
  35. void luacall(std::ptrdiff_t argcount, std::ptrdiff_t) {
  36. #if SOL_LUA_VERSION_I_ >= 504
  37. int nresults;
  38. stats = static_cast<call_status>(lua_resume(lua_state(), nullptr, static_cast<int>(argcount), &nresults));
  39. #else
  40. stats = static_cast<call_status>(lua_resume(lua_state(), nullptr, static_cast<int>(argcount)));
  41. #endif
  42. }
  43. template <std::size_t... I, typename... Ret>
  44. auto invoke(types<Ret...>, std::index_sequence<I...>, std::ptrdiff_t n) {
  45. luacall(n, sizeof...(Ret));
  46. return stack::pop<std::tuple<Ret...>>(lua_state());
  47. }
  48. template <std::size_t I, typename Ret>
  49. Ret invoke(types<Ret>, std::index_sequence<I>, std::ptrdiff_t n) {
  50. luacall(n, 1);
  51. return stack::pop<Ret>(lua_state());
  52. }
  53. template <std::size_t I>
  54. void invoke(types<void>, std::index_sequence<I>, std::ptrdiff_t n) {
  55. luacall(n, 0);
  56. }
  57. protected_function_result invoke(types<>, std::index_sequence<>, std::ptrdiff_t n) {
  58. int firstreturn = 1;
  59. luacall(n, LUA_MULTRET);
  60. int poststacksize = lua_gettop(this->lua_state());
  61. int returncount = poststacksize - (firstreturn - 1);
  62. if (error()) {
  63. if (m_error_handler.valid()) {
  64. string_view err = stack::get<string_view>(this->lua_state(), poststacksize);
  65. m_error_handler.push();
  66. stack::push(this->lua_state(), err);
  67. lua_call(lua_state(), 1, 1);
  68. }
  69. return protected_function_result(this->lua_state(), lua_absindex(this->lua_state(), -1), 1, returncount, status());
  70. }
  71. return protected_function_result(this->lua_state(), firstreturn, returncount, returncount, status());
  72. }
  73. public:
  74. using base_t::lua_state;
  75. basic_packaged_coroutine() = default;
  76. template <typename T,
  77. meta::enable<meta::neg<std::is_same<meta::unqualified_t<T>, basic_packaged_coroutine>>,
  78. meta::neg<std::is_base_of<proxy_base_tag, meta::unqualified_t<T>>>, meta::neg<std::is_same<base_t, stack_reference>>,
  79. meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  80. basic_packaged_coroutine(T&& r) noexcept
  81. : base_t(std::forward<T>(r)), m_error_handler(detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  82. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  83. if (!is_function<meta::unqualified_t<T>>::value) {
  84. auto pp = stack::push_pop(*this);
  85. constructor_handler handler {};
  86. stack::check<basic_packaged_coroutine>(lua_state(), -1, handler);
  87. }
  88. #endif // Safety
  89. }
  90. basic_packaged_coroutine(const basic_packaged_coroutine& other) = default;
  91. basic_packaged_coroutine& operator=(const basic_packaged_coroutine&) = default;
  92. basic_packaged_coroutine(basic_packaged_coroutine&& other) noexcept
  93. : base_t(std::move(other)), m_error_handler(this->lua_state(), std::move(other.m_error_handler)) {
  94. }
  95. basic_packaged_coroutine& operator=(basic_packaged_coroutine&& other) noexcept {
  96. base_t::operator=(std::move(other));
  97. // must change the state, since it could change on the coroutine type
  98. m_error_handler.abandon();
  99. m_error_handler = handler_t(this->lua_state(), std::move(other.m_error_handler));
  100. return *this;
  101. }
  102. basic_packaged_coroutine(const basic_function<base_t>& b) noexcept
  103. : basic_packaged_coroutine(b, detail::get_default_handler<reference, is_main_threaded_v<base_t>>(b.lua_state())) {
  104. }
  105. basic_packaged_coroutine(basic_function<base_t>&& b) noexcept
  106. : basic_packaged_coroutine(std::move(b), detail::get_default_handler<reference, is_main_threaded_v<base_t>>(b.lua_state())) {
  107. }
  108. basic_packaged_coroutine(const basic_function<base_t>& b, handler_t eh) noexcept : base_t(b), m_error_handler(std::move(eh)) {
  109. }
  110. basic_packaged_coroutine(basic_function<base_t>&& b, handler_t eh) noexcept : base_t(std::move(b)), m_error_handler(std::move(eh)) {
  111. }
  112. basic_packaged_coroutine(const stack_reference& r) noexcept
  113. : basic_packaged_coroutine(r.lua_state(), r.stack_index(), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  114. }
  115. basic_packaged_coroutine(stack_reference&& r) noexcept
  116. : basic_packaged_coroutine(r.lua_state(), r.stack_index(), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(r.lua_state())) {
  117. }
  118. basic_packaged_coroutine(const stack_reference& r, handler_t eh) noexcept : basic_packaged_coroutine(r.lua_state(), r.stack_index(), std::move(eh)) {
  119. }
  120. basic_packaged_coroutine(stack_reference&& r, handler_t eh) noexcept : basic_packaged_coroutine(r.lua_state(), r.stack_index(), std::move(eh)) {
  121. }
  122. template <typename Super>
  123. basic_packaged_coroutine(const proxy_base<Super>& p)
  124. : basic_packaged_coroutine(p, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(p.lua_state())) {
  125. }
  126. template <typename Super>
  127. basic_packaged_coroutine(proxy_base<Super>&& p)
  128. : basic_packaged_coroutine(std::move(p), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(p.lua_state())) {
  129. }
  130. template <typename Proxy, typename HandlerReference,
  131. meta::enable<std::is_base_of<proxy_base_tag, meta::unqualified_t<Proxy>>, meta::neg<is_lua_index<meta::unqualified_t<HandlerReference>>>> = meta::enabler>
  132. basic_packaged_coroutine(Proxy&& p, HandlerReference&& eh) : basic_packaged_coroutine(detail::force_cast<base_t>(p), std::forward<HandlerReference>(eh)) {
  133. }
  134. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  135. basic_packaged_coroutine(lua_State* L, T&& r) noexcept
  136. : basic_packaged_coroutine(L, std::forward<T>(r), detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  137. }
  138. template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
  139. basic_packaged_coroutine(lua_State* L, T&& r, handler_t eh) : base_t(L, std::forward<T>(r)), m_error_handler(std::move(eh)) {
  140. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  141. auto pp = stack::push_pop(*this);
  142. constructor_handler handler {};
  143. stack::check<basic_packaged_coroutine>(lua_state(), -1, handler);
  144. #endif // Safety
  145. }
  146. basic_packaged_coroutine(lua_nil_t n) : base_t(n), m_error_handler(n) {
  147. }
  148. basic_packaged_coroutine(lua_State* L, int index = -1)
  149. : basic_packaged_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  150. }
  151. basic_packaged_coroutine(lua_State* L, int index, handler_t eh) : base_t(L, index), m_error_handler(std::move(eh)) {
  152. #ifdef SOL_SAFE_REFERENCES
  153. constructor_handler handler {};
  154. stack::check<basic_packaged_coroutine>(L, index, handler);
  155. #endif // Safety
  156. }
  157. basic_packaged_coroutine(lua_State* L, absolute_index index)
  158. : basic_packaged_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  159. }
  160. basic_packaged_coroutine(lua_State* L, absolute_index index, handler_t eh) : base_t(L, index), m_error_handler(std::move(eh)) {
  161. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  162. constructor_handler handler {};
  163. stack::check<basic_packaged_coroutine>(L, index, handler);
  164. #endif // Safety
  165. }
  166. basic_packaged_coroutine(lua_State* L, raw_index index)
  167. : basic_packaged_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  168. }
  169. basic_packaged_coroutine(lua_State* L, raw_index index, handler_t eh) : base_t(L, index), m_error_handler(std::move(eh)) {
  170. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  171. constructor_handler handler {};
  172. stack::check<basic_packaged_coroutine>(L, index, handler);
  173. #endif // Safety
  174. }
  175. basic_packaged_coroutine(lua_State* L, ref_index index)
  176. : basic_packaged_coroutine(L, index, detail::get_default_handler<reference, is_main_threaded<base_t>::value>(L)) {
  177. }
  178. basic_packaged_coroutine(lua_State* L, ref_index index, handler_t eh) : base_t(L, index), m_error_handler(std::move(eh)) {
  179. #if SOL_IS_ON(SOL_SAFE_REFERENCES)
  180. auto pp = stack::push_pop(*this);
  181. constructor_handler handler {};
  182. stack::check<basic_packaged_coroutine>(lua_state(), -1, handler);
  183. #endif // Safety
  184. }
  185. call_status status() const noexcept {
  186. return stats;
  187. }
  188. bool error() const noexcept {
  189. call_status cs = status();
  190. return cs != call_status::ok && cs != call_status::yielded;
  191. }
  192. bool runnable() const noexcept {
  193. return base_t::valid() && (status() == call_status::yielded);
  194. }
  195. reference error_handler() const noexcept {
  196. return reference(m_L, registry_index(m_error_handler.index));
  197. }
  198. set_error_handler(reference new_error_handler) noexcept {
  199. this->m_error_handler = stateless_reference(this->m_L, std::move(new_error_handler));
  200. }
  201. explicit operator bool() const noexcept {
  202. return runnable();
  203. }
  204. template <typename... Args>
  205. protected_function_result operator()(Args&&... args) {
  206. return call<>(std::forward<Args>(args)...);
  207. }
  208. template <typename... Ret, typename... Args>
  209. decltype(auto) operator()(types<Ret...>, Args&&... args) {
  210. return call<Ret...>(std::forward<Args>(args)...);
  211. }
  212. template <typename... Ret, typename... Args>
  213. decltype(auto) call(Args&&... args) {
  214. // some users screw up coroutine.create
  215. // and try to use it with sol::coroutine without ever calling the first resume in Lua
  216. // this makes the stack incompatible with other kinds of stacks: protect against this
  217. // make sure coroutines don't screw us over
  218. base_t::push();
  219. int pushcount = stack::multi_push_reference(lua_state(), std::forward<Args>(args)...);
  220. return invoke(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), pushcount);
  221. }
  222. };
  223. #endif
  224. } // namespace sol
  225. #endif // SOL_PACKAGED_COROUTINE_HPP