reference.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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_REFERENCE_HPP
  19. #define SOL_REFERENCE_HPP
  20. #include <sol/types.hpp>
  21. #include <sol/stack_reference.hpp>
  22. #include <functional>
  23. namespace sol {
  24. namespace detail {
  25. inline const char (&default_main_thread_name())[9] {
  26. static const char name[9] = "sol.\xF0\x9F\x93\x8C";
  27. return name;
  28. }
  29. } // namespace detail
  30. namespace stack {
  31. inline void remove(lua_State* L_, int rawindex, int count) {
  32. if (count < 1)
  33. return;
  34. int top = lua_gettop(L_);
  35. if (top < 1) {
  36. return;
  37. }
  38. if (rawindex == -count || top == rawindex) {
  39. // Slice them right off the top
  40. lua_pop(L_, static_cast<int>(count));
  41. return;
  42. }
  43. // Remove each item one at a time using stack operations
  44. // Probably slower, maybe, haven't benchmarked,
  45. // but necessary
  46. int index = lua_absindex(L_, rawindex);
  47. if (index < 0) {
  48. index = lua_gettop(L_) + (index + 1);
  49. }
  50. int last = index + count;
  51. for (int i = index; i < last; ++i) {
  52. lua_remove(L_, index);
  53. }
  54. }
  55. struct push_popper_at {
  56. lua_State* L;
  57. int index;
  58. int count;
  59. push_popper_at(lua_State* L_, int index_ = -1, int count_ = 1) : L(L_), index(index_), count(count_) {
  60. }
  61. ~push_popper_at() {
  62. remove(L, index, count);
  63. }
  64. };
  65. template <bool top_level>
  66. struct push_popper_n {
  67. lua_State* L;
  68. int pop_count;
  69. push_popper_n(lua_State* L_, int pop_count_) : L(L_), pop_count(pop_count_) {
  70. }
  71. push_popper_n(const push_popper_n&) = delete;
  72. push_popper_n(push_popper_n&&) = default;
  73. push_popper_n& operator=(const push_popper_n&) = delete;
  74. push_popper_n& operator=(push_popper_n&&) = default;
  75. ~push_popper_n() {
  76. lua_pop(L, pop_count);
  77. }
  78. };
  79. template <>
  80. struct push_popper_n<true> {
  81. push_popper_n(lua_State*, int) {
  82. }
  83. };
  84. template <bool, typename T, typename = void>
  85. struct push_popper {
  86. using Tu = meta::unqualified_t<T>;
  87. T m_object;
  88. int m_index;
  89. push_popper(T object_) noexcept : m_object(object_), m_index(lua_absindex(m_object.lua_state(), -m_object.push())) {
  90. }
  91. int index_of(const Tu&) const noexcept {
  92. return m_index;
  93. }
  94. ~push_popper() {
  95. m_object.pop();
  96. }
  97. };
  98. template <typename T, typename C>
  99. struct push_popper<true, T, C> {
  100. using Tu = meta::unqualified_t<T>;
  101. push_popper(T) noexcept {
  102. }
  103. int index_of(const Tu&) const noexcept {
  104. return -1;
  105. }
  106. ~push_popper() {
  107. }
  108. };
  109. template <typename T>
  110. struct push_popper<false, T, std::enable_if_t<is_stack_based_v<meta::unqualified_t<T>>>> {
  111. using Tu = meta::unqualified_t<T>;
  112. push_popper(T) noexcept {
  113. }
  114. int index_of(const Tu& object_) const noexcept {
  115. return object_.stack_index();
  116. }
  117. ~push_popper() {
  118. }
  119. };
  120. template <bool, typename T, typename = void>
  121. struct stateless_push_popper {
  122. using Tu = meta::unqualified_t<T>;
  123. lua_State* m_L;
  124. T m_object;
  125. int m_index;
  126. stateless_push_popper(lua_State* L_, T object_) noexcept : m_L(L_), m_object(object_), m_index(lua_absindex(m_L, -m_object.push(m_L))) {
  127. }
  128. int index_of(const Tu&) const noexcept {
  129. return m_index;
  130. }
  131. ~stateless_push_popper() {
  132. m_object.pop(m_L);
  133. }
  134. };
  135. template <typename T, typename C>
  136. struct stateless_push_popper<true, T, C> {
  137. using Tu = meta::unqualified_t<T>;
  138. stateless_push_popper(lua_State*, T) noexcept {
  139. }
  140. int index_of(lua_State*, const Tu&) const noexcept {
  141. return -1;
  142. }
  143. ~stateless_push_popper() {
  144. }
  145. };
  146. template <typename T>
  147. struct stateless_push_popper<false, T, std::enable_if_t<is_stack_based_v<meta::unqualified_t<T>>>> {
  148. using Tu = meta::unqualified_t<T>;
  149. lua_State* m_L;
  150. stateless_push_popper(lua_State* L_, T) noexcept : m_L(L_) {
  151. }
  152. int index_of(const Tu& object_) const noexcept {
  153. return object_.stack_index();
  154. }
  155. ~stateless_push_popper() {
  156. }
  157. };
  158. template <bool top_level = false, typename T>
  159. push_popper<top_level, T> push_pop(T&& x) {
  160. return push_popper<top_level, T>(std::forward<T>(x));
  161. }
  162. template <bool top_level = false, typename T>
  163. stateless_push_popper<top_level, T> push_pop(lua_State* L_, T&& object_) {
  164. return stateless_push_popper<top_level, T>(L_, std::forward<T>(object_));
  165. }
  166. template <typename T>
  167. push_popper_at push_pop_at(T&& object_) {
  168. int push_count = object_.push();
  169. lua_State* L = object_.lua_state();
  170. return push_popper_at(L, lua_absindex(L, -push_count), push_count);
  171. }
  172. template <bool top_level = false>
  173. push_popper_n<top_level> pop_n(lua_State* L_, int pop_count_) {
  174. return push_popper_n<top_level>(L_, pop_count_);
  175. }
  176. } // namespace stack
  177. inline lua_State* main_thread(lua_State* L_, lua_State* backup_if_unsupported_ = nullptr) {
  178. #if SOL_LUA_VERSION_I_ < 502
  179. if (L_ == nullptr)
  180. return backup_if_unsupported_;
  181. lua_getglobal(L_, detail::default_main_thread_name());
  182. auto pp = stack::pop_n(L_, 1);
  183. if (type_of(L_, -1) == type::thread) {
  184. return lua_tothread(L_, -1);
  185. }
  186. return backup_if_unsupported_;
  187. #else
  188. if (L_ == nullptr)
  189. return backup_if_unsupported_;
  190. lua_rawgeti(L_, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
  191. lua_State* Lmain = lua_tothread(L_, -1);
  192. lua_pop(L_, 1);
  193. return Lmain;
  194. #endif // Lua 5.2+ has the main thread unqualified_getter
  195. }
  196. namespace detail {
  197. struct no_safety_tag {
  198. } inline constexpr no_safety {};
  199. template <bool b>
  200. inline lua_State* pick_main_thread(lua_State* L_, lua_State* backup_if_unsupported = nullptr) {
  201. (void)L_;
  202. (void)backup_if_unsupported;
  203. if (b) {
  204. return main_thread(L_, backup_if_unsupported);
  205. }
  206. return L_;
  207. }
  208. } // namespace detail
  209. class stateless_reference {
  210. private:
  211. template <bool o_main_only>
  212. friend class basic_reference;
  213. int ref = LUA_NOREF;
  214. int copy_ref(lua_State* L_) const noexcept {
  215. if (ref == LUA_NOREF)
  216. return LUA_NOREF;
  217. push(L_);
  218. return luaL_ref(L_, LUA_REGISTRYINDEX);
  219. }
  220. lua_State* copy_assign_ref(lua_State* L_, lua_State* rL, const stateless_reference& r) {
  221. if (valid(L_)) {
  222. deref(L_);
  223. }
  224. ref = r.copy_ref(L_);
  225. return rL;
  226. }
  227. lua_State* move_assign(lua_State* L_, lua_State* rL, stateless_reference&& r) {
  228. if (valid(L_)) {
  229. deref(L_);
  230. }
  231. ref = r.ref;
  232. r.ref = LUA_NOREF;
  233. return rL;
  234. }
  235. protected:
  236. int stack_index() const noexcept {
  237. return -1;
  238. }
  239. stateless_reference(lua_State* L_, global_tag_t) noexcept {
  240. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  241. luaL_checkstack(L_, 1, "not enough Lua stack space to push this reference value");
  242. #endif // make sure stack doesn't overflow
  243. lua_pushglobaltable(L_);
  244. ref = luaL_ref(L_, LUA_REGISTRYINDEX);
  245. }
  246. stateless_reference(int raw_ref_index) noexcept : ref(raw_ref_index) {
  247. }
  248. public:
  249. stateless_reference() noexcept = default;
  250. stateless_reference(lua_nil_t) noexcept : stateless_reference() {
  251. }
  252. stateless_reference(const stack_reference& r) noexcept : stateless_reference(r.lua_state(), r.stack_index()) {
  253. }
  254. stateless_reference(stack_reference&& r) noexcept : stateless_reference(r.lua_state(), r.stack_index()) {
  255. }
  256. stateless_reference(lua_State* L_, const stateless_reference& r) noexcept {
  257. if (r.ref == LUA_REFNIL) {
  258. ref = LUA_REFNIL;
  259. return;
  260. }
  261. if (r.ref == LUA_NOREF || L_ == nullptr) {
  262. ref = LUA_NOREF;
  263. return;
  264. }
  265. ref = r.copy_ref(L_);
  266. }
  267. stateless_reference(lua_State* L_, stateless_reference&& r) noexcept {
  268. if (r.ref == LUA_REFNIL) {
  269. ref = LUA_REFNIL;
  270. return;
  271. }
  272. if (r.ref == LUA_NOREF || L_ == nullptr) {
  273. ref = LUA_NOREF;
  274. return;
  275. }
  276. ref = r.ref;
  277. r.ref = LUA_NOREF;
  278. }
  279. stateless_reference(lua_State* L_, const stack_reference& r) noexcept {
  280. if (L_ == nullptr || r.lua_state() == nullptr || r.get_type() == type::none) {
  281. ref = LUA_NOREF;
  282. return;
  283. }
  284. if (r.get_type() == type::lua_nil) {
  285. ref = LUA_REFNIL;
  286. return;
  287. }
  288. if (L_ != r.lua_state() && !detail::xmovable(L_, r.lua_state())) {
  289. return;
  290. }
  291. r.push(L_);
  292. ref = luaL_ref(L_, LUA_REGISTRYINDEX);
  293. }
  294. stateless_reference(lua_State* L_, const stateless_stack_reference& r) noexcept : stateless_reference(L_, r.stack_index()) {
  295. }
  296. stateless_reference(lua_State* L_, int index = -1) noexcept {
  297. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  298. luaL_checkstack(L_, 1, "not enough Lua stack space to push this reference value");
  299. #endif // make sure stack doesn't overflow
  300. lua_pushvalue(L_, index);
  301. ref = luaL_ref(L_, LUA_REGISTRYINDEX);
  302. }
  303. stateless_reference(lua_State* L_, absolute_index index_) noexcept : stateless_reference(L_, index_.index) {
  304. }
  305. stateless_reference(lua_State* L_, ref_index index_) noexcept {
  306. lua_rawgeti(L_, LUA_REGISTRYINDEX, index_.index);
  307. ref = luaL_ref(L_, LUA_REGISTRYINDEX);
  308. }
  309. stateless_reference(lua_State*, lua_nil_t) noexcept {
  310. }
  311. ~stateless_reference() noexcept = default;
  312. stateless_reference(const stateless_reference& o) noexcept = delete;
  313. stateless_reference& operator=(const stateless_reference& r) noexcept = delete;
  314. stateless_reference(stateless_reference&& o) noexcept : ref(o.ref) {
  315. o.ref = LUA_NOREF;
  316. }
  317. stateless_reference& operator=(stateless_reference&& o) noexcept {
  318. ref = o.ref;
  319. o.ref = LUA_NOREF;
  320. return *this;
  321. }
  322. int push(lua_State* L_) const noexcept {
  323. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  324. luaL_checkstack(L_, 1, "not enough Lua stack space to push this reference value");
  325. #endif // make sure stack doesn't overflow
  326. lua_rawgeti(L_, LUA_REGISTRYINDEX, ref);
  327. return 1;
  328. }
  329. void pop(lua_State* L_, int n = 1) const noexcept {
  330. lua_pop(L_, n);
  331. }
  332. int registry_index() const noexcept {
  333. return ref;
  334. }
  335. void reset(lua_State* L_) noexcept {
  336. if (valid(L_)) {
  337. deref(L_);
  338. }
  339. ref = LUA_NOREF;
  340. }
  341. void reset(lua_State* L_, int index_) noexcept {
  342. reset(L_);
  343. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  344. luaL_checkstack(L_, 1, "not enough Lua stack space to push this reference value");
  345. #endif // make sure stack doesn't overflow
  346. lua_pushvalue(L_, index_);
  347. ref = luaL_ref(L_, LUA_REGISTRYINDEX);
  348. }
  349. bool valid(lua_State*) const noexcept {
  350. return !(ref == LUA_NOREF || ref == LUA_REFNIL);
  351. }
  352. const void* pointer(lua_State* L_) const noexcept {
  353. int si = push(L_);
  354. const void* vp = lua_topointer(L_, -si);
  355. lua_pop(L_, si);
  356. return vp;
  357. }
  358. type get_type(lua_State* L_) const noexcept {
  359. int p = push(L_);
  360. int result = lua_type(L_, -1);
  361. pop(L_, p);
  362. return static_cast<type>(result);
  363. }
  364. void abandon(lua_State* = nullptr) {
  365. ref = LUA_NOREF;
  366. }
  367. void deref(lua_State* L_) const noexcept {
  368. luaL_unref(L_, LUA_REGISTRYINDEX, ref);
  369. }
  370. stateless_reference copy(lua_State* L_) const noexcept {
  371. if (!valid(L_)) {
  372. return {};
  373. }
  374. return stateless_reference(copy_ref(L_));
  375. }
  376. void copy_assign(lua_State* L_, const stateless_reference& right) noexcept {
  377. if (valid(L_)) {
  378. deref(L_);
  379. }
  380. if (!right.valid(L_)) {
  381. return;
  382. }
  383. ref = right.copy_ref(L_);
  384. }
  385. bool equals(lua_State* L_, const stateless_reference& r) const noexcept {
  386. auto ppl = stack::push_pop(L_, *this);
  387. auto ppr = stack::push_pop(L_, r);
  388. return lua_compare(L_, -1, -2, LUA_OPEQ) == 1;
  389. }
  390. bool equals(lua_State* L_, const stateless_stack_reference& r) const noexcept {
  391. auto ppl = stack::push_pop(L_, *this);
  392. return lua_compare(L_, -1, r.stack_index(), LUA_OPEQ) == 1;
  393. }
  394. bool equals(lua_State* L_, lua_nil_t) const noexcept {
  395. return valid(L_);
  396. }
  397. };
  398. template <bool main_only = false>
  399. class basic_reference : public stateless_reference {
  400. private:
  401. template <bool o_main_only>
  402. friend class basic_reference;
  403. lua_State* luastate = nullptr; // non-owning
  404. template <bool r_main_only>
  405. void copy_assign_complex(const basic_reference<r_main_only>& r) {
  406. if (valid()) {
  407. deref();
  408. }
  409. if (r.ref == LUA_REFNIL) {
  410. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  411. ref = LUA_REFNIL;
  412. return;
  413. }
  414. if (r.ref == LUA_NOREF) {
  415. luastate = r.luastate;
  416. ref = LUA_NOREF;
  417. return;
  418. }
  419. if (detail::xmovable(lua_state(), r.lua_state())) {
  420. r.push(lua_state());
  421. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  422. return;
  423. }
  424. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  425. ref = r.copy_ref();
  426. }
  427. template <bool r_main_only>
  428. void move_assign(basic_reference<r_main_only>&& r) {
  429. if (valid()) {
  430. deref();
  431. }
  432. if (r.ref == LUA_REFNIL) {
  433. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  434. ref = LUA_REFNIL;
  435. return;
  436. }
  437. if (r.ref == LUA_NOREF) {
  438. luastate = r.luastate;
  439. ref = LUA_NOREF;
  440. return;
  441. }
  442. if (detail::xmovable(lua_state(), r.lua_state())) {
  443. r.push(lua_state());
  444. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  445. return;
  446. }
  447. luastate = detail::pick_main_thread < main_only && !r_main_only > (r.lua_state(), r.lua_state());
  448. ref = r.ref;
  449. r.ref = LUA_NOREF;
  450. r.luastate = nullptr;
  451. }
  452. protected:
  453. basic_reference(lua_State* L_, global_tag_t) noexcept : basic_reference(detail::pick_main_thread<main_only>(L_, L_), global_tag, global_tag) {
  454. }
  455. basic_reference(lua_State* L_, global_tag_t, global_tag_t) noexcept : stateless_reference(L_, global_tag), luastate(L_) {
  456. }
  457. basic_reference(lua_State* oL, const basic_reference<!main_only>& o) noexcept : stateless_reference(oL, o), luastate(oL) {
  458. }
  459. void deref() const noexcept {
  460. return stateless_reference::deref(lua_state());
  461. }
  462. int copy_ref() const noexcept {
  463. return copy_ref(lua_state());
  464. }
  465. int copy_ref(lua_State* L_) const noexcept {
  466. return stateless_reference::copy_ref(L_);
  467. }
  468. public:
  469. basic_reference() noexcept = default;
  470. basic_reference(lua_nil_t) noexcept : basic_reference() {
  471. }
  472. basic_reference(const stack_reference& r) noexcept : basic_reference(r.lua_state(), r.stack_index()) {
  473. }
  474. basic_reference(stack_reference&& r) noexcept : basic_reference(r.lua_state(), r.stack_index()) {
  475. }
  476. template <bool r_main_only>
  477. basic_reference(lua_State* L_, const basic_reference<r_main_only>& r) noexcept : luastate(detail::pick_main_thread<main_only>(L_, L_)) {
  478. if (r.ref == LUA_REFNIL) {
  479. ref = LUA_REFNIL;
  480. return;
  481. }
  482. if (r.ref == LUA_NOREF || lua_state() == nullptr) {
  483. ref = LUA_NOREF;
  484. return;
  485. }
  486. if (detail::xmovable(lua_state(), r.lua_state())) {
  487. r.push(lua_state());
  488. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  489. return;
  490. }
  491. ref = r.copy_ref();
  492. }
  493. template <bool r_main_only>
  494. basic_reference(lua_State* L_, basic_reference<r_main_only>&& r) noexcept : luastate(detail::pick_main_thread<main_only>(L_, L_)) {
  495. if (r.ref == LUA_REFNIL) {
  496. ref = LUA_REFNIL;
  497. return;
  498. }
  499. if (r.ref == LUA_NOREF || lua_state() == nullptr) {
  500. ref = LUA_NOREF;
  501. return;
  502. }
  503. if (detail::xmovable(lua_state(), r.lua_state())) {
  504. r.push(lua_state());
  505. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  506. return;
  507. }
  508. ref = r.ref;
  509. r.ref = LUA_NOREF;
  510. r.luastate = nullptr;
  511. }
  512. basic_reference(lua_State* L_, const stack_reference& r) noexcept : luastate(detail::pick_main_thread<main_only>(L_, L_)) {
  513. if (lua_state() == nullptr || r.lua_state() == nullptr || r.get_type() == type::none) {
  514. ref = LUA_NOREF;
  515. return;
  516. }
  517. if (r.get_type() == type::lua_nil) {
  518. ref = LUA_REFNIL;
  519. return;
  520. }
  521. if (lua_state() != r.lua_state() && !detail::xmovable(lua_state(), r.lua_state())) {
  522. return;
  523. }
  524. r.push(lua_state());
  525. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  526. }
  527. basic_reference(lua_State* L_, int index = -1) noexcept : luastate(detail::pick_main_thread<main_only>(L_, L_)) {
  528. // use L_ to stick with that state's execution stack
  529. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  530. luaL_checkstack(L_, 1, "not enough Lua stack space to push this reference value");
  531. #endif // make sure stack doesn't overflow
  532. lua_pushvalue(L_, index);
  533. ref = luaL_ref(L_, LUA_REGISTRYINDEX);
  534. }
  535. basic_reference(lua_State* L_, ref_index index) noexcept : luastate(detail::pick_main_thread<main_only>(L_, L_)) {
  536. lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, index.index);
  537. ref = luaL_ref(lua_state(), LUA_REGISTRYINDEX);
  538. }
  539. basic_reference(lua_State* L_, lua_nil_t) noexcept : luastate(detail::pick_main_thread<main_only>(L_, L_)) {
  540. }
  541. ~basic_reference() noexcept {
  542. if (lua_state() == nullptr || ref == LUA_NOREF)
  543. return;
  544. deref();
  545. }
  546. basic_reference(const basic_reference& o) noexcept : stateless_reference(o.copy_ref()), luastate(o.lua_state()) {
  547. }
  548. basic_reference(basic_reference&& o) noexcept : stateless_reference(std::move(o)), luastate(o.lua_state()) {
  549. o.luastate = nullptr;
  550. }
  551. basic_reference(const basic_reference<!main_only>& o) noexcept
  552. : basic_reference(detail::pick_main_thread<main_only>(o.lua_state(), o.lua_state()), o) {
  553. }
  554. basic_reference(basic_reference<!main_only>&& o) noexcept
  555. : stateless_reference(std::move(o)), luastate(detail::pick_main_thread<main_only>(o.lua_state(), o.lua_state())) {
  556. o.luastate = nullptr;
  557. o.ref = LUA_NOREF;
  558. }
  559. basic_reference& operator=(basic_reference&& r) noexcept {
  560. move_assign(std::move(r));
  561. return *this;
  562. }
  563. basic_reference& operator=(const basic_reference& r) noexcept {
  564. copy_assign_complex(r);
  565. return *this;
  566. }
  567. basic_reference& operator=(basic_reference<!main_only>&& r) noexcept {
  568. move_assign(std::move(r));
  569. return *this;
  570. }
  571. basic_reference& operator=(const basic_reference<!main_only>& r) noexcept {
  572. copy_assign_complex(r);
  573. return *this;
  574. }
  575. basic_reference& operator=(const lua_nil_t&) noexcept {
  576. reset();
  577. return *this;
  578. }
  579. template <typename Super>
  580. basic_reference& operator=(proxy_base<Super>&& r);
  581. template <typename Super>
  582. basic_reference& operator=(const proxy_base<Super>& r);
  583. int push() const noexcept {
  584. return push(lua_state());
  585. }
  586. void reset() noexcept {
  587. stateless_reference::reset(luastate);
  588. luastate = nullptr;
  589. }
  590. int push(lua_State* L_) const noexcept {
  591. #if SOL_IS_ON(SOL_SAFE_STACK_CHECK)
  592. luaL_checkstack(L_, 1, "not enough Lua stack space to push this reference value");
  593. #endif // make sure stack doesn't overflow
  594. if (lua_state() == nullptr) {
  595. lua_pushnil(L_);
  596. return 1;
  597. }
  598. lua_rawgeti(lua_state(), LUA_REGISTRYINDEX, ref);
  599. if (L_ != lua_state()) {
  600. lua_xmove(lua_state(), L_, 1);
  601. }
  602. return 1;
  603. }
  604. void pop() const noexcept {
  605. pop(lua_state());
  606. }
  607. void pop(lua_State* L_, int n = 1) const noexcept {
  608. stateless_reference::pop(L_, n);
  609. }
  610. int registry_index() const noexcept {
  611. return stateless_reference::registry_index();
  612. }
  613. bool valid() const noexcept {
  614. return stateless_reference::valid(lua_state());
  615. }
  616. bool valid(lua_State* L_) const noexcept {
  617. return stateless_reference::valid(L_);
  618. }
  619. const void* pointer() const noexcept {
  620. return stateless_reference::pointer(lua_state());
  621. }
  622. explicit operator bool() const noexcept {
  623. return valid();
  624. }
  625. type get_type() const noexcept {
  626. return stateless_reference::get_type(lua_state());
  627. }
  628. lua_State* lua_state() const noexcept {
  629. return luastate;
  630. }
  631. };
  632. template <bool lb, bool rb>
  633. inline bool operator==(const basic_reference<lb>& l, const basic_reference<rb>& r) noexcept {
  634. auto ppl = stack::push_pop(l);
  635. auto ppr = stack::push_pop(r);
  636. return lua_compare(l.lua_state(), -1, -2, LUA_OPEQ) == 1;
  637. }
  638. template <bool lb, bool rb>
  639. inline bool operator!=(const basic_reference<lb>& l, const basic_reference<rb>& r) noexcept {
  640. return !operator==(l, r);
  641. }
  642. template <bool lb>
  643. inline bool operator==(const basic_reference<lb>& l, const stack_reference& r) noexcept {
  644. auto ppl = stack::push_pop(l);
  645. return lua_compare(l.lua_state(), -1, r.stack_index(), LUA_OPEQ) == 1;
  646. }
  647. template <bool lb>
  648. inline bool operator!=(const basic_reference<lb>& l, const stack_reference& r) noexcept {
  649. return !operator==(l, r);
  650. }
  651. template <bool rb>
  652. inline bool operator==(const stack_reference& l, const basic_reference<rb>& r) noexcept {
  653. auto ppr = stack::push_pop(r);
  654. return lua_compare(l.lua_state(), -1, r.stack_index(), LUA_OPEQ) == 1;
  655. }
  656. template <bool rb>
  657. inline bool operator!=(const stack_reference& l, const basic_reference<rb>& r) noexcept {
  658. return !operator==(l, r);
  659. }
  660. template <bool lb>
  661. inline bool operator==(const basic_reference<lb>& lhs, const lua_nil_t&) noexcept {
  662. return !lhs.valid();
  663. }
  664. template <bool rb>
  665. inline bool operator==(const lua_nil_t&, const basic_reference<rb>& rhs) noexcept {
  666. return !rhs.valid();
  667. }
  668. template <bool lb>
  669. inline bool operator!=(const basic_reference<lb>& lhs, const lua_nil_t&) noexcept {
  670. return lhs.valid();
  671. }
  672. template <bool rb>
  673. inline bool operator!=(const lua_nil_t&, const basic_reference<rb>& rhs) noexcept {
  674. return rhs.valid();
  675. }
  676. inline bool operator==(const stateless_reference& l, const stateless_reference& r) noexcept {
  677. return l.registry_index() == r.registry_index();
  678. }
  679. inline bool operator!=(const stateless_reference& l, const stateless_reference& r) noexcept {
  680. return l.registry_index() != r.registry_index();
  681. }
  682. inline bool operator==(const stateless_reference& lhs, const lua_nil_t&) noexcept {
  683. return lhs.registry_index() == LUA_REFNIL;
  684. }
  685. inline bool operator==(const lua_nil_t&, const stateless_reference& rhs) noexcept {
  686. return rhs.registry_index() == LUA_REFNIL;
  687. }
  688. inline bool operator!=(const stateless_reference& lhs, const lua_nil_t&) noexcept {
  689. return lhs.registry_index() != LUA_REFNIL;
  690. }
  691. inline bool operator!=(const lua_nil_t&, const stateless_reference& rhs) noexcept {
  692. return rhs.registry_index() != LUA_REFNIL;
  693. }
  694. struct stateless_reference_equals : public stateless_stack_reference_equals {
  695. using is_transparent = std::true_type;
  696. stateless_reference_equals(lua_State* L_) noexcept : stateless_stack_reference_equals(L_) {
  697. }
  698. bool operator()(const lua_nil_t& lhs, const stateless_reference& rhs) const noexcept {
  699. return rhs.equals(lua_state(), lhs);
  700. }
  701. bool operator()(const stateless_reference& lhs, const lua_nil_t& rhs) const noexcept {
  702. return lhs.equals(lua_state(), rhs);
  703. }
  704. bool operator()(const stateless_reference& lhs, const stateless_reference& rhs) const noexcept {
  705. return lhs.equals(lua_state(), rhs);
  706. }
  707. };
  708. struct reference_equals : public stack_reference_equals {
  709. using is_transparent = std::true_type;
  710. template <bool rb>
  711. bool operator()(const lua_nil_t& lhs, const basic_reference<rb>& rhs) const noexcept {
  712. return lhs == rhs;
  713. }
  714. template <bool lb>
  715. bool operator()(const basic_reference<lb>& lhs, const lua_nil_t& rhs) const noexcept {
  716. return lhs == rhs;
  717. }
  718. template <bool lb, bool rb>
  719. bool operator()(const basic_reference<lb>& lhs, const basic_reference<rb>& rhs) const noexcept {
  720. return lhs == rhs;
  721. }
  722. template <bool lb>
  723. bool operator()(const basic_reference<lb>& lhs, const stack_reference& rhs) const noexcept {
  724. return lhs == rhs;
  725. }
  726. template <bool rb>
  727. bool operator()(const stack_reference& lhs, const basic_reference<rb>& rhs) const noexcept {
  728. return lhs == rhs;
  729. }
  730. };
  731. struct stateless_reference_hash : public stateless_stack_reference_hash {
  732. using argument_type = stateless_reference;
  733. using result_type = std::size_t;
  734. using is_transparent = std::true_type;
  735. stateless_reference_hash(lua_State* L_) noexcept : stateless_stack_reference_hash(L_) {
  736. }
  737. result_type operator()(const stateless_reference& lhs) const noexcept {
  738. std::hash<const void*> h;
  739. return h(lhs.pointer(lua_state()));
  740. }
  741. };
  742. struct reference_hash : public stack_reference_hash {
  743. using argument_type = reference;
  744. using result_type = std::size_t;
  745. using is_transparent = std::true_type;
  746. template <bool lb>
  747. result_type operator()(const basic_reference<lb>& lhs) const noexcept {
  748. std::hash<const void*> h;
  749. return h(lhs.pointer());
  750. }
  751. };
  752. } // namespace sol
  753. #endif // SOL_REFERENCE_HPP