sol2/include/sol/detail/pairs.hpp

70 lines
2.5 KiB
C++

// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this Spermission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SOL_DETAIL_PAIRS_HPP
#define SOL_DETAIL_PAIRS_HPP
#include <sol/version.hpp>
#include <sol/stack.hpp>
#include <sol/stack_reference.hpp>
#include <sol/assert.hpp>
namespace sol { namespace stack { namespace stack_detail {
inline int c_lua_next(lua_State* L_) noexcept {
stack_reference table_stack_ref(L_, raw_index(1));
stateless_stack_reference key_stack_ref(L_, raw_index(2));
int result = lua_next(table_stack_ref.lua_state(), table_stack_ref.stack_index());
if (result == 0) {
stack::push(L_, lua_nil);
return 1;
}
return 2;
}
inline int readonly_pairs(lua_State* L_) noexcept {
int pushed = 0;
stack::get_field<true, false>(L_, "next");
if (!stack::check<unsafe_function>(L_, -1)) {
// we do not have the "next" function in the global namespace
// from the "table" global entiry, use our own
pushed += stack::push(L_, &c_lua_next);
}
else {
pushed += 1;
}
int metatable_exists = lua_getmetatable(L_, 1);
sol_c_assert(metatable_exists == 1);
const auto& index_key = to_string(sol::meta_function::index);
lua_getfield(L_, lua_gettop(L_), index_key.c_str());
lua_remove(L_, -2);
pushed += 1;
pushed += stack::push(L_, lua_nil);
return pushed;
}
}}} // sol::stack::stack_detail
#endif // SOL_DETAIL_PAIRS_HPP