Use absolute indices in the places where it's necessary.

This commit is contained in:
ThePhD 2016-05-03 16:49:53 -04:00
parent 2aa5ab77a8
commit 1c61773354
5 changed files with 13 additions and 10 deletions

View File

@ -60,8 +60,9 @@ inline int construct(Match&& matchfx, lua_State* L, int fxarity, int start) {
template <typename T, typename... TypeLists> template <typename T, typename... TypeLists>
inline int construct(lua_State* L) { inline int construct(lua_State* L) {
static const auto& meta = usertype_traits<T>::metatable; static const auto& meta = usertype_traits<T>::metatable;
call_syntax syntax = stack::get_call_syntax(L, meta, 1); int argcount = lua_gettop(L);
int argcount = lua_gettop(L) - static_cast<int>(syntax); call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, meta, 1) : call_syntax::dot;
argcount -= static_cast<int>(syntax);
T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T))); T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
T*& referencepointer = *pointerpointer; T*& referencepointer = *pointerpointer;

View File

@ -114,7 +114,8 @@ public:
} }
bool valid () const { bool valid () const {
auto p = stack::probe_get_field<std::is_same<meta::Unqualified<Table>, global_table>::value>(tbl.lua_state(), key); stack::push_pop(tbl);
auto p = stack::probe_get_field<std::is_same<meta::Unqualified<Table>, global_table>::value>(tbl.lua_state(), key, lua_gettop(tbl.lua_state()));
lua_pop(tbl.lua_state(), p.levels); lua_pop(tbl.lua_state(), p.levels);
return p; return p;
} }

View File

@ -148,7 +148,7 @@ public:
inline bool operator== (const reference& l, const reference& r) { inline bool operator== (const reference& l, const reference& r) {
auto ppl = stack::push_pop(l); auto ppl = stack::push_pop(l);
auto ppr = stack::push_pop(r); auto ppr = stack::push_pop(r);
return lua_compare(l.lua_state(), -1, -2, LUA_OPEQ) == 0; return lua_compare(l.lua_state(), -1, -2, LUA_OPEQ) == 1;
} }
inline bool operator!= (const reference& l, const reference& r) { inline bool operator!= (const reference& l, const reference& r) {

View File

@ -168,13 +168,13 @@ struct field_setter<std::tuple<Args...>, b, C> {
template <bool g, std::size_t I, typename Key, typename Value> template <bool g, std::size_t I, typename Key, typename Value>
void apply(std::index_sequence<I>, lua_State* L, Key&& keys, Value&& value, int tableindex) { void apply(std::index_sequence<I>, lua_State* L, Key&& keys, Value&& value, int tableindex) {
I < 1 ? I < 1 ?
set_field<g>(L, detail::forward_get<I>(keys), std::forward<Value>(value)) : set_field<g>(L, detail::forward_get<I>(keys), std::forward<Value>(value), tableindex) :
set_field<g>(L, detail::forward_get<I>(keys), std::forward<Value>(value), tableindex); set_field<g>(L, detail::forward_get<I>(keys), std::forward<Value>(value));
} }
template <bool g, std::size_t I0, std::size_t I1, std::size_t... I, typename Keys, typename Value> template <bool g, std::size_t I0, std::size_t I1, std::size_t... I, typename Keys, typename Value>
void apply(std::index_sequence<I0, I1, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) { void apply(std::index_sequence<I0, I1, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
get_field<g>(L, detail::forward_get<I0>(keys), tableindex); I0 < 1 ? get_field<g>(L, detail::forward_get<I0>(keys), tableindex) : get_field<g>(L, detail::forward_get<I0>(keys), -1);
apply<false>(std::index_sequence<I1, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), -1); apply<false>(std::index_sequence<I1, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), -1);
} }

View File

@ -89,7 +89,8 @@ class basic_table_core : public base_t {
auto pp = stack::push_pop<top_level && (is_global<decltype(detail::forward_get<I * 2>(pairs))...>::value)>(*this); auto pp = stack::push_pop<top_level && (is_global<decltype(detail::forward_get<I * 2>(pairs))...>::value)>(*this);
void(detail::swallow{ (stack::set_field<top_level>(base_t::lua_state(), void(detail::swallow{ (stack::set_field<top_level>(base_t::lua_state(),
detail::forward_get<I * 2>(pairs), detail::forward_get<I * 2>(pairs),
detail::forward_get<I * 2 + 1>(pairs) detail::forward_get<I * 2 + 1>(pairs),
lua_gettop(base_t::lua_state())
), 0)... }); ), 0)... });
} }
@ -108,7 +109,7 @@ class basic_table_core : public base_t {
template <bool global, typename T, std::size_t I, typename Key> template <bool global, typename T, std::size_t I, typename Key>
decltype(auto) traverse_get_deep_optional( int& popcount, Key&& key ) const { decltype(auto) traverse_get_deep_optional( int& popcount, Key&& key ) const {
typedef decltype(stack::get<T>(base_t::lua_state())) R; typedef decltype(stack::get<T>(base_t::lua_state())) R;
auto p = stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), -1); auto p = stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
popcount += p.levels; popcount += p.levels;
if (!p.success) if (!p.success)
return R(nullopt); return R(nullopt);
@ -117,7 +118,7 @@ class basic_table_core : public base_t {
template <bool global, typename T, std::size_t I, typename Key, typename... Keys> template <bool global, typename T, std::size_t I, typename Key, typename... Keys>
decltype(auto) traverse_get_deep_optional( int& popcount, Key&& key, Keys&&... keys ) const { decltype(auto) traverse_get_deep_optional( int& popcount, Key&& key, Keys&&... keys ) const {
auto p = I > 0 ? stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), - 1) : stack::probe_get_field<global>( base_t::lua_state( ), std::forward<Key>( key ) ); auto p = I > 0 ? stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), -1) : stack::probe_get_field<global>( base_t::lua_state( ), std::forward<Key>( key ), lua_gettop(base_t::lua_state()));
popcount += p.levels; popcount += p.levels;
if (!p.success) if (!p.success)
return T(nullopt); return T(nullopt);