pairs.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Spermission 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_DETAIL_PAIRS_HPP
  19. #define SOL_DETAIL_PAIRS_HPP
  20. #include <sol/version.hpp>
  21. #include <sol/stack.hpp>
  22. #include <sol/stack_reference.hpp>
  23. #include <sol/protected_function.hpp>
  24. #include <sol/assert.hpp>
  25. #include <optional>
  26. namespace sol { namespace stack { namespace stack_detail {
  27. inline bool maybe_push_lua_next_function(lua_State* L_) {
  28. stack::get_field<true, false>(L_, "next");
  29. bool is_next = stack::check<protected_function>(L_);
  30. if (is_next) {
  31. return true;
  32. }
  33. stack::get_field<true, false>(L_, "table");
  34. stack::record tracking{};
  35. if (!stack::loose_table_check(L_, -1, &no_panic, tracking)) {
  36. return false;
  37. }
  38. lua_getfield(L_, -1, "next");
  39. bool is_table_next_func = stack::check<protected_function>(L_, -1);
  40. if (is_table_next_func) {
  41. return true;
  42. }
  43. lua_pop(L_, 1);
  44. return false;
  45. }
  46. inline std::optional<protected_function> find_lua_next_function(lua_State* L_) {
  47. if (maybe_push_lua_next_function(L_)) {
  48. return stack::pop<protected_function>(L_);
  49. }
  50. return std::nullopt;
  51. }
  52. inline int c_lua_next(lua_State* L_) noexcept {
  53. stack_reference table_stack_ref(L_, raw_index(1));
  54. stateless_stack_reference key_stack_ref(L_, raw_index(2));
  55. int result = lua_next(table_stack_ref.lua_state(), table_stack_ref.stack_index());
  56. if (result == 0) {
  57. stack::push(L_, lua_nil);
  58. return 1;
  59. }
  60. return 2;
  61. }
  62. inline int readonly_pairs(lua_State* L_) noexcept {
  63. int pushed = 0;
  64. if (!maybe_push_lua_next_function(L_)) {
  65. // we do not have the "next" function in the global namespace
  66. // from the "table" global entiry, use our own
  67. pushed += stack::push(L_, &c_lua_next);
  68. }
  69. else {
  70. pushed += 1;
  71. }
  72. int metatable_exists = lua_getmetatable(L_, 1);
  73. SOL_ASSERT(metatable_exists == 1);
  74. const auto& index_key = to_string(sol::meta_function::index);
  75. lua_getfield(L_, lua_gettop(L_), index_key.c_str());
  76. lua_remove(L_, -2);
  77. pushed += 1;
  78. pushed += stack::push(L_, lua_nil);
  79. return pushed;
  80. }
  81. }}} // sol::stack::stack_detail
  82. #endif // SOL_DETAIL_PAIRS_HPP