coroutine.hpp 10 KB

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