diff --git a/sol/function_types_allocator.hpp b/sol/function_types_allocator.hpp index 30c451dd..1c900aac 100644 --- a/sol/function_types_allocator.hpp +++ b/sol/function_types_allocator.hpp @@ -44,7 +44,7 @@ struct constructor_match { template int operator()(meta::Bool, types, Index, types r, types a, lua_State* L, int, int start) const { default_construct func{}; - return stack::typed_call(r, a, func, L, start, obj); + return stack::call_into_lua(r, a, func, L, start, obj); } }; } // detail @@ -120,7 +120,7 @@ struct usertype_constructor_function : base_function { userdataref.pop(); auto& func = std::get(overloads); - stack::typed_call(r, a, func, L, start, function_detail::implicit_wrapper(obj)); + stack::call_into_lua(r, a, func, L, start, function_detail::implicit_wrapper(obj)); userdataref.push(); luaL_getmetatable(L, &meta[0]); diff --git a/sol/function_types_member.hpp b/sol/function_types_member.hpp index cd033472..28ffd3cb 100644 --- a/sol/function_types_member.hpp +++ b/sol/function_types_member.hpp @@ -80,7 +80,7 @@ struct member_function : public base_function { member_function(Tm&& m, Args&&... args): fx(std::forward(m), std::forward(args)...) {} virtual int operator()(lua_State* L) override { - return stack::typed_call(meta::tuple_types(), args_types(), fx, L, 1); + return stack::call_into_lua(meta::tuple_types(), args_types(), fx, L, 1); } }; } // function_detail diff --git a/sol/function_types_overload.hpp b/sol/function_types_overload.hpp index 56836b49..0a5c4a57 100644 --- a/sol/function_types_overload.hpp +++ b/sol/function_types_overload.hpp @@ -96,7 +96,7 @@ struct overloaded_function : base_function { template int call(meta::Bool, types, Index, types r, types a, lua_State* L, int, int start) { auto& func = std::get(overloads); - return stack::typed_call(r, a, func, L, start); + return stack::call_into_lua(r, a, func, L, start); } virtual int operator()(lua_State* L) override { @@ -117,7 +117,7 @@ struct usertype_overloaded_function : base_function { int call(meta::Bool, types, Index, types r, types a, lua_State* L, int, int start) { auto& func = std::get(overloads); func.item = detail::ptr(stack::get(L, 1)); - return stack::typed_call(r, a, func, L, start); + return stack::call_into_lua(r, a, func, L, start); } virtual int operator()(lua_State* L) override { diff --git a/sol/function_types_static.hpp b/sol/function_types_static.hpp index 535f02d5..d57e5503 100644 --- a/sol/function_types_static.hpp +++ b/sol/function_types_static.hpp @@ -34,7 +34,7 @@ struct static_function { static int call(lua_State* L) { auto udata = stack::stack_detail::get_as_upvalues(L); function_type* fx = udata.first; - int r = stack::typed_call(meta::tuple_types(), typename traits_type::args_type(), fx, L, 1); + int r = stack::call_into_lua(meta::tuple_types(), typename traits_type::args_type(), fx, L, 1); return r; } @@ -54,7 +54,7 @@ struct static_member_function { function_type& memfx = memberdata.first; T& item = *objdata.first; auto fx = [&item, &memfx](auto&&... args) -> typename traits_type::return_type { return (item.*memfx)(std::forward(args)...); }; - return stack::typed_call(meta::tuple_types(), typename traits_type::args_type(), fx, L, 1); + return stack::call_into_lua(meta::tuple_types(), typename traits_type::args_type(), fx, L, 1); } int operator()(lua_State* L) { diff --git a/sol/reference.hpp b/sol/reference.hpp index 83f3baaf..bd3dac6b 100644 --- a/sol/reference.hpp +++ b/sol/reference.hpp @@ -27,19 +27,19 @@ namespace sol { namespace stack { template -struct push_pop { +struct push_popper { T t; - push_pop (T x) : t(x) { t.push(); } - ~push_pop() { t.pop(); } + push_popper (T x) : t(x) { t.push(); } + ~push_popper() { t.pop(); } }; template -struct push_pop { - push_pop (T) {} - ~push_pop() {} +struct push_popper { + push_popper (T) {} + ~push_popper() {} }; template -push_pop push_popper(T&& x) { - return push_pop(std::forward(x)); +push_popper push_pop(T&& x) { + return push_popper(std::forward(x)); } } // stack diff --git a/sol/stack.hpp b/sol/stack.hpp index 363e5518..ba7343ed 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -831,11 +831,6 @@ inline R call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... return call(tr, ta, L, 1, std::forward(fx), std::forward(args)...); } -template ::value>> -inline R top_call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { - return call(tr, ta, L, static_cast(lua_gettop(L) - sizeof...(Args)), std::forward(fx), std::forward(args)...); -} - template inline void call(types tr, types ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { typedef typename types::indices args_indices; @@ -847,13 +842,18 @@ inline void call(types tr, types ta, lua_State* L, Fx&& fx, FxArg call(tr, ta, L, 1, std::forward(fx), std::forward(args)...); } +template ::value>> +inline R call_from_top(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { + return call(tr, ta, L, static_cast(lua_gettop(L) - sizeof...(Args)), std::forward(fx), std::forward(args)...); +} + template -inline void top_call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { +inline void call_from_top(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { call(tr, ta, L, static_cast(lua_gettop(L) - sizeof...(Args)), std::forward(fx), std::forward(args)...); } template -inline int typed_call(types tr, types ta, Fx&& fx, lua_State* L, int start, FxArgs&&... fxargs) { +inline int call_into_lua(types tr, types ta, Fx&& fx, lua_State* L, int start, FxArgs&&... fxargs) { call(tr, ta, L, start, fx, std::forward(fxargs)...); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); @@ -861,7 +861,7 @@ inline int typed_call(types tr, types ta, Fx&& fx, lua_State* L, } template>::value>> -inline int typed_call(types, types ta, Fx&& fx, lua_State* L, int start, FxArgs&&... fxargs) { +inline int call_into_lua(types, types ta, Fx&& fx, lua_State* L, int start, FxArgs&&... fxargs) { decltype(auto) r = call(types>(), ta, L, start, fx, std::forward(fxargs)...); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); diff --git a/sol/state_view.hpp b/sol/state_view.hpp index 32a04c1c..9c1eed42 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -211,15 +211,6 @@ public: return result; } - template - table create_table(int narr = 0, int nrec = sizeof...(Args), Args&&... args) { - lua_createtable(L, narr, nrec); - table result(L); - result.set(std::forward(args)...); - lua_pop(L, 1); - return result; - } - global_table global() const { return globals; } diff --git a/sol/table_core.hpp b/sol/table_core.hpp index deb5a0d9..21450a3d 100644 --- a/sol/table_core.hpp +++ b/sol/table_core.hpp @@ -39,7 +39,7 @@ class table_core : public reference { template auto tuple_get( types, std::index_sequence, Keys&& keys ) const -> decltype(stack::pop>(nullptr)){ - auto pp = stack::push_popper(keys))...>::value>(*this); + auto pp = stack::push_pop(keys))...>::value>(*this); int tableindex = lua_gettop(lua_state()); void(detail::swallow{ ( stack::get_field(lua_state(), std::get(keys), tableindex), 0)... }); return stack::pop>( lua_state() ); @@ -47,14 +47,14 @@ class table_core : public reference { template decltype(auto) tuple_get( types, std::index_sequence, Keys&& keys ) const { - auto pp = stack::push_popper(keys))>::value>(*this); + auto pp = stack::push_pop(keys))>::value>(*this); stack::get_field( lua_state( ), std::get( keys ) ); return stack::pop( lua_state( ) ); } template void tuple_set( std::index_sequence, Pairs&& pairs ) { - auto pp = stack::push_popper(pairs))...>::value>(*this); + auto pp = stack::push_pop(pairs))...>::value>(*this); void(detail::swallow{ (stack::set_field(lua_state(), std::get(pairs), std::get(pairs)), 0)... }); } @@ -97,14 +97,14 @@ public: template decltype(auto) traverse_get( Keys&&... keys ) const { - auto pp = stack::push_popper::value>(*this); + auto pp = stack::push_pop::value>(*this); struct clean { lua_State* L; clean(lua_State* L) : L(L) {} ~clean() { lua_pop(L, static_cast(sizeof...(Keys))); } } c(lua_state()); return traverse_get_deep(std::forward(keys)...); } template table_core& traverse_set( Keys&&... keys ) { - auto pp = stack::push_popper::value>(*this); + auto pp = stack::push_pop::value>(*this); traverse_set_deep(std::forward(keys)...); lua_pop(lua_state(), static_cast(sizeof...(Keys)-2)); return *this; @@ -128,7 +128,7 @@ public: template void for_each( Fx&& fx ) const { - auto pp = stack::push_popper( *this ); + auto pp = stack::push_pop( *this ); stack::push( lua_state( ), nil ); while ( lua_next( this->lua_state( ), -2 ) ) { sol::object key( lua_state( ), -2 ); @@ -139,7 +139,7 @@ public: } size_t size( ) const { - auto pp = stack::push_popper( *this ); + auto pp = stack::push_pop( *this ); return lua_rawlen(lua_state(), -1); } @@ -211,6 +211,23 @@ private: set(std::forward(key), function_pack>(std::forward(args)...)); } }; +namespace stack { +inline table create_table(lua_State* L, int narr = 0, int nrec = 0) { + lua_createtable(L, narr, nrec); + table result(L); + lua_pop(L, 1); + return result; +} + +template +inline table create_table(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) { + lua_createtable(L, narr, nrec); + table result(L); + result.set(std::forward(key), std::forward(value), std::forward(args)...); + lua_pop(L, 1); + return result; +} +} // stack } // sol #endif // SOL_TABLE_CORE_HPP