// The MIT License (MIT) // Copyright (c) 2013-2016 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 permission 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_STACK_HPP #define SOL_STACK_HPP #include "error.hpp" #include "reference.hpp" #include "tuple.hpp" #include "traits.hpp" #include "usertype_traits.hpp" #include "inheritance.hpp" #include "overload.hpp" #include #include #include #include namespace sol { namespace stack { template struct field_getter; template struct field_setter; template struct getter; template struct popper; template struct pusher; template::value, typename = void> struct checker; template inline int push(lua_State* L, T&& t, Args&&... args) { return pusher>{}.push(L, std::forward(t), std::forward(args)...); } // overload allows to use a pusher of a specific type, but pass in any kind of args template inline int push(lua_State* L, Arg&& arg, Args&&... args) { return pusher>{}.push(L, std::forward(arg), std::forward(args)...); } inline int push_args(lua_State*) { // do nothing return 0; } template inline int push_args(lua_State* L, T&& t, Args&&... args) { int pushcount = push(L, std::forward(t)); void(sol::detail::swallow{(pushcount += sol::stack::push(L, std::forward(args)), 0)... }); return pushcount; } template inline decltype(auto) get(lua_State* L, int index = -1) { return getter>{}.get(L, index); } template inline decltype(auto) pop(lua_State* L) { return popper>{}.pop(L); } template bool check(lua_State* L, int index, Handler&& handler) { typedef meta::Unqualified Tu; checker c; // VC++ has a bad warning here: shut it up (void)c; return c.check(L, index, std::forward(handler)); } template bool check(lua_State* L, int index) { auto handler = type_panic; return check(L, index, handler); } template void get_field(lua_State* L, Key&& key) { field_getter, global>{}.get(L, std::forward(key)); } template void get_field(lua_State* L, Key&& key, int tableindex) { field_getter, global>{}.get(L, std::forward(key), tableindex); } template void set_field(lua_State* L, Key&& key, Value&& value) { field_setter, global>{}.set(L, std::forward(key), std::forward(value)); } template void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) { field_setter, global>{}.set(L, std::forward(key), std::forward(value), tableindex); } namespace stack_detail { const bool default_check_arguments = #ifdef SOL_CHECK_ARGUMENTS true; #else false; #endif template static int push_upvalues(lua_State* L, TCont&& cont) { int n = 0; for(auto& c : cont) { if(releasemem) { stack::push(L, c.release()); } else { stack::push(L, c.get()); } ++n; } return n; } template struct userdata_pusher { template static void push (lua_State* L, Key&& metatablekey, Args&&... args) { // Basically, we store all user0data like this: // If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new // data in the first sizeof(T*) bytes, and then however many bytes it takes to // do the actual object. Things that are std::ref or plain T* are stored as // just the sizeof(T*), and nothing else. T** pdatum = static_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); T** referencepointer = pdatum; T*& referencereference = *pdatum; T* allocationtarget = reinterpret_cast(pdatum + 1); referencereference = allocationtarget; std::allocator alloc{}; alloc.construct(allocationtarget, std::forward(args)...); luaL_getmetatable(L, &metatablekey[0]); lua_setmetatable(L, -2); } }; template struct userdata_pusher { template static void push (lua_State* L, Key&& metatablekey, Args&&... args) { T** pdatum = static_cast(lua_newuserdata(L, sizeof(T*))); std::allocator alloc{}; alloc.construct(pdatum, std::forward(args)...); luaL_getmetatable(L, &metatablekey[0]); lua_setmetatable(L, -2); } }; template inline int push_tuple(std::index_sequence, lua_State* L, T&& tuplen) { int pushcount = 0; void(detail::swallow{(pushcount += sol::stack::push(L, std::get(tuplen)), '\0')... }); return pushcount; } template inline int push_confirmed_userdata(lua_State* L, Key&& metatablekey, Args&&... args) { userdata_pusher{}.push(L, std::forward(metatablekey), std::forward(args)...); return 1; } template inline int push_userdata_pointer(lua_State* L, Key&& metatablekey) { return push_confirmed_userdata(L, std::forward(metatablekey)); } template >> = 0> inline int push_userdata_pointer(lua_State* L, Key&& metatablekey, Arg&& arg) { if (arg == nullptr) return push(L, nil); return push_confirmed_userdata(L, std::forward(metatablekey), std::forward(arg)); } template >> = 0> inline int push_userdata_pointer(lua_State* L, Key&& metatablekey, Arg&& arg) { return push_confirmed_userdata(L, std::forward(metatablekey), std::forward(arg)); } template inline int push_userdata_pointer(lua_State* L, Key&& metatablekey, Arg0&& arg0, Arg1&& arg1, Args&&... args) { return push_confirmed_userdata(L, std::forward(metatablekey), std::forward(arg0), std::forward(arg1), std::forward(args)...); } template> = 0> inline int push_userdata(lua_State* L, Key&& metatablekey, Args&&... args) { return push_confirmed_userdata(L, std::forward(metatablekey), std::forward(args)...); } template> = 0> inline int push_userdata(lua_State* L, Key&& metatablekey, Args&&... args) { return push_userdata_pointer(L, std::forward(metatablekey), std::forward(args)...); } } // stack_detail template struct getter { static T& get(lua_State* L, int index = -1) { return getter{}.get(L, index); } }; template struct getter::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tonumber(L, index)); } }; template struct getter, std::is_signed>::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tointeger(L, index)); } }; template struct getter, std::is_unsigned>::value>> { static T get(lua_State* L, int index = -1) { return static_cast(lua_tointeger(L, index)); } }; template struct getter::value>> { static T get(lua_State* L, int index = -1) { return T(L, index); } }; template struct getter { static T* get_no_nil(lua_State* L, int index = -1) { void** pudata = static_cast(lua_touserdata(L, index)); void* udata = *pudata; #ifndef SOL_NO_EXCEPTIONS if (luaL_getmetafield(L, -1, &detail::base_class_check_key[0]) != 0) { void* basecastdata = stack::get(L); detail::throw_cast basecast = (detail::throw_cast)basecastdata; // use the casting function to properly adjust the pointer for the desired T udata = detail::catch_cast( udata, basecast ); lua_pop(L, 1); } #elif !defined(SOL_NO_RTTI) if (luaL_getmetafield(L, -1, &detail::base_class_check_key[0]) != 0) { detail::inheritance_cast_function ic = (detail::inheritance_cast_function)stack::get(L); // use the casting function to properly adjust the pointer for the desired T udata = ic(udata, typeid(T)); lua_pop(L, 1); } #else // Lol, inheritance could never work like this #endif // No Runtime Type Information || Exceptions T* obj = static_cast(udata); return obj; } static T* get(lua_State* L, int index = -1) { type t = type_of(L, index); if (t == type::nil) return nullptr; return get_no_nil(L, index); } }; template struct getter { static T& get(lua_State* L, int index = -1) { return *getter::get_no_nil(L, index); } }; template struct getter> { static T& get(lua_State* L, int index = -1) { return getter{}.get(L, index); } }; template<> struct getter { static type get(lua_State *L, int index){ return static_cast(lua_type(L, index)); } }; template<> struct getter { static bool get(lua_State* L, int index) { return lua_toboolean(L, index) != 0; } }; template<> struct getter { static std::string get(lua_State* L, int index = -1) { std::size_t len; auto str = lua_tolstring(L, index, &len); return { str, len }; } }; template<> struct getter { static const char* get(lua_State* L, int index = -1) { return lua_tostring(L, index); } }; template<> struct getter { static nil_t get(lua_State*, int = -1) { return nil_t{ }; } }; template<> struct getter { static lua_CFunction get(lua_State* L, int index = -1) { return lua_tocfunction(L, index); } }; template<> struct getter { static c_closure get(lua_State* L, int index = -1) { return c_closure(lua_tocfunction(L, index), -1); } }; template<> struct getter { static userdata get(lua_State* L, int index = -1) { return{ lua_touserdata(L, index) }; } }; template<> struct getter { static light_userdata get(lua_State* L, int index = -1) { return{ lua_touserdata(L, index) }; } }; template<> struct getter { static upvalue get(lua_State* L, int index = 1) { return{ lua_touserdata(L, lua_upvalueindex(index)) }; } }; template<> struct getter { static void* get(lua_State* L, int index = -1) { return lua_touserdata(L, index); } }; template struct getter> { template static decltype(auto) apply(std::index_sequence, lua_State* L, int index = -1) { return std::tuple(L, index + I))...>(stack::get(L, index + I)...); } static decltype(auto) get(lua_State* L, int index = -1) { return apply(std::index_sequence_for(), L, index); } }; template struct checker { template static bool check (lua_State* L, int index, const Handler& handler) { const type indextype = type_of(L, index); bool success = expected == indextype; if (!success) { // expected type, actual type handler(L, index, expected, indextype); } return success; } }; template struct checker { template static bool check (lua_State* L, int index, const Handler& handler) { const type indextype = type_of(L, index); // Allow nil to be transformed to nullptr if (indextype == type::nil) { return true; } return checker{}.check(L, indextype, index, handler); } }; template struct checker { template static bool check (lua_State* L, type indextype, int index, const Handler& handler) { if (indextype != type::userdata) { handler(L, index, type::userdata, indextype); return false; } if (lua_getmetatable(L, index) == 0) { handler(L, index, type::userdata, indextype); return false; } luaL_getmetatable(L, &usertype_traits::metatable[0]); const type expectedmetatabletype = get(L); if (expectedmetatabletype == type::nil) { lua_pop(L, 2); handler(L, index, type::userdata, indextype); return false; } bool success = lua_rawequal(L, -1, -2) == 1; #ifndef SOL_NO_EXCEPTIONS if (!success) { lua_getfield(L, -2, &detail::base_class_check_key[0]); void* basecastdata = stack::get(L); detail::throw_cast basecast = (detail::throw_cast)basecastdata; success |= detail::catch_check(basecast); lua_pop(L, 1); } #elif !defined(SOL_NO_RTTI) if (!success) { lua_getfield(L, -2, &detail::base_class_check_key[0]); detail::inheritance_check_function ic = (detail::inheritance_check_function)stack::get(L); success |= ic(typeid(T)); lua_pop(L, 1); } #endif // No Runtime Type Information || Exceptions lua_pop(L, 2); return success; } template static bool check (lua_State* L, int index, const Handler& handler) { const type indextype = type_of(L, index); return check(L, indextype, index, handler); } }; template struct popper { inline decltype(auto) pop(lua_State* L) { decltype(auto) r = get(L); lua_pop(L, 1); return r; } }; template struct popper> { inline decltype(auto) pop(lua_State* L) { decltype(auto) r = get>(L, lua_gettop(L) - sizeof...(Args) + 1); lua_pop(L, static_cast(sizeof...(Args))); return r; } }; template struct pusher { static int push(lua_State* L, T& t) { return stack_detail::push_userdata(L, usertype_traits::metatable, t); } static int push(lua_State* L, T&& t) { return stack_detail::push_userdata(L, usertype_traits::metatable, std::move(t)); } }; template struct pusher::value>> { static int push(lua_State* L, const T& value) { lua_pushnumber(L, value); return 1; } }; template struct pusher, std::is_signed>::value>> { static int push(lua_State* L, const T& value) { lua_pushinteger(L, static_cast(value)); return 1; } }; template struct pusher, std::is_unsigned>::value>> { static int push(lua_State* L, const T& value) { typedef std::make_signed_t signed_int; return stack::push(L, static_cast(value)); } }; template struct pusher, meta::Not>>::value>> { static int push(lua_State* L, const T& cont) { lua_createtable(L, static_cast(cont.size()), 0); unsigned index = 1; for(auto&& i : cont) { // push the index pusher{}.push(L, index++); // push the value pusher>{}.push(L, i); // set the table lua_settable(L, -3); } return 1; } }; template struct pusher, meta::has_key_value_pair>::value>> { static int push(lua_State* L, const T& cont) { lua_createtable(L, static_cast(cont.size()), 0); for(auto&& pair : cont) { pusher>{}.push(L, pair.first); pusher>{}.push(L, pair.second); lua_settable(L, -3); } return 1; } }; template struct pusher::value>> { static int push(lua_State*, T& ref) { return ref.push(); } }; template struct pusher { static int push(lua_State* L, T* obj) { return stack_detail::push_userdata(L, usertype_traits::metatable, obj); } }; template struct pusher> { static int push(lua_State* L, const std::reference_wrapper& t) { return stack::push(L, std::addressof(t.get())); } }; template<> struct pusher { static int push(lua_State* L, bool b) { lua_pushboolean(L, b); return 1; } }; template<> struct pusher { static int push(lua_State* L, nil_t) { lua_pushnil(L); return 1; } }; template<> struct pusher> { static int push(lua_State* L, lua_CFunction func, int n = 0) { lua_pushcclosure(L, func, n); return 1; } }; template<> struct pusher { static int push(lua_State* L, lua_CFunction func, int n = 0) { lua_pushcclosure(L, func, n); return 1; } }; template<> struct pusher { static int push(lua_State* L, c_closure closure) { lua_pushcclosure(L, closure.c_function, closure.upvalues); return 1; } }; template<> struct pusher { static int push(lua_State* L, void* userdata) { lua_pushlightuserdata(L, userdata); return 1; } }; template<> struct pusher { static int push(lua_State* L, upvalue upv) { lua_pushlightuserdata(L, upv); return 1; } }; template<> struct pusher { static int push(lua_State* L, light_userdata userdata) { lua_pushlightuserdata(L, userdata); return 1; } }; template<> struct pusher { static int push(lua_State* L, userdata data) { void** ud = static_cast(lua_newuserdata(L, sizeof(void*))); *ud = data.value; return 1; } }; template<> struct pusher { static int push(lua_State* L, const char* str) { lua_pushlstring(L, str, std::char_traits::length(str)); return 1; } }; template struct pusher { static int push(lua_State* L, const char (&str)[N]) { lua_pushlstring(L, str, N - 1); return 1; } }; template<> struct pusher { static int push(lua_State* L, const std::string& str) { lua_pushlstring(L, str.c_str(), str.size()); return 1; } }; template struct pusher> { template static int push(lua_State* L, Tuple&& tuplen) { return stack_detail::push_tuple(std::index_sequence_for(), L, std::forward(tuplen)); } }; template struct field_getter { template void get(lua_State* L, Key&& key, int tableindex = -2) { push( L, std::forward( key ) ); lua_gettable( L, tableindex ); } }; template struct field_getter, b, C> { template void apply(std::index_sequence, lua_State* L, Key&& key, int tableindex) { get_field(L, std::get(key), tableindex); void(detail::swallow{ (get_field(L, std::get(key)), 0)... }); reference saved(L, -1); lua_pop(L, static_cast(sizeof...(I) + 1)); saved.push(); } template void get(lua_State* L, Key&& key, int tableindex = -2) { apply(std::index_sequence_for(), L, std::forward(key), tableindex); } }; template struct field_getter::value>> { template void get(lua_State* L, Key&& key, int = -1) { lua_getglobal(L, &key[0]); } }; template struct field_getter::value>> { template void get(lua_State* L, Key&& key, int tableindex = -1) { lua_getfield(L, tableindex, &key[0]); } }; #if SOL_LUA_VERSION >= 503 template struct field_getter::value>> { template void get(lua_State* L, Key&& key, int tableindex = -1) { lua_geti(L, tableindex, static_cast(key)); } }; #endif // Lua 5.3.x template struct field_setter { template void set(lua_State* L, Key&& key, Value&& value, int tableindex = -3) { push(L, std::forward(key)); push(L, std::forward(value)); lua_settable(L, tableindex); } }; template struct field_setter::value>> { template void set(lua_State* L, Key&& key, Value&& value, int = -2) { push(L, std::forward(value)); lua_setglobal(L, &key[0]); } }; template struct field_setter::value>> { template void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) { push(L, std::forward(value)); lua_setfield(L, tableindex, &key[0]); } }; #if SOL_LUA_VERSION >= 503 template struct field_setter::value>> { template void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) { push(L, std::forward(value)); lua_seti(L, tableindex, static_cast(key)); } }; #endif // Lua 5.3.x namespace stack_detail { template inline int push_as_upvalues(lua_State* L, T& item) { typedef std::decay_t TValue; const static std::size_t itemsize = sizeof(TValue); const static std::size_t voidsize = sizeof(void*); const static std::size_t voidsizem1 = voidsize - 1; const static std::size_t data_t_count = (sizeof(TValue) + voidsizem1) / voidsize; typedef std::array data_t; data_t data{{}}; std::memcpy(&data[0], std::addressof(item), itemsize); int pushcount = 0; for(auto&& v : data) { pushcount += push(L, upvalue(v)); } return pushcount; } template inline std::pair get_as_upvalues(lua_State* L, int index = 1) { const static std::size_t data_t_count = (sizeof(T)+(sizeof(void*)-1)) / sizeof(void*); typedef std::array data_t; data_t voiddata{ {} }; for(std::size_t i = 0, d = 0; d < sizeof(T); ++i, d += sizeof(void*)) { voiddata[i] = get(L, index++); } return std::pair(*reinterpret_cast(static_cast(voiddata.data())), index); } template struct check_arguments { template static bool check(types, std::index_sequence, lua_State* L, int firstargument) { if (!stack::check(L, firstargument + I0)) return false; return check(types(), std::index_sequence(), L, firstargument); } static bool check(types<>, std::index_sequence<>, lua_State*, int) { return true; } }; template <> struct check_arguments { template static bool check(types, std::index_sequence, lua_State*, int) { return true; } }; template ::value>> inline R call(types, types ta, std::index_sequence tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { check_arguments{}.check(ta, tai, L, start); return fx(std::forward(args)..., stack::get(L, start + I)...); } template inline void call(types, types ta, std::index_sequence tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { check_arguments{}.check(ta, tai, L, start); fx(std::forward(args)..., stack::get(L, start + I)...); } } // stack_detail inline void remove( lua_State* L, int index, int count ) { if ( count < 1 ) return; int top = lua_gettop( L ); if ( index == -1 || top == index ) { // Slice them right off the top lua_pop( L, static_cast(count) ); return; } // Remove each item one at a time using stack operations // Probably slower, maybe, haven't benchmarked, // but necessary if ( index < 0 ) { index = lua_gettop( L ) + (index + 1); } int last = index + count; for ( int i = index; i < last; ++i ) { lua_remove( L, i ); } } template ::value>> inline R call(types tr, types ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) { typedef typename types::indices args_indices; return stack_detail::call(tr, ta, args_indices(), L, start, std::forward(fx), std::forward(args)...); } template ::value>> inline R call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { return call(tr, ta, L, 1, 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; stack_detail::call(tr, ta, args_indices(), L, start, std::forward(fx), std::forward(args)...); } template inline void call(types tr, types ta, lua_State* L, Fx&& fx, FxArgs&&... args) { 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 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 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); return 0; } template>::value>> 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); return push(L, std::forward(r)); } inline call_syntax get_call_syntax(lua_State* L, const std::string& meta) { luaL_getmetatable(L, meta.c_str()); if (lua_compare(L, -1, -2, LUA_OPEQ) == 1) { lua_pop(L, 1); return call_syntax::colon; } lua_pop(L, 1); return call_syntax::dot; } inline void luajit_exception_handler(lua_State* L, int(*handler)(lua_State*, lua_CFunction) = detail::c_trampoline) { #ifdef SOL_LUAJIT lua_pushlightuserdata(L, (void*)handler); luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON); lua_pop(L, 1); #else (void)L; (void)handler; #endif } inline void luajit_exception_off(lua_State* L) { #ifdef SOL_LUAJIT luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_OFF); #else (void)L; #endif } } // stack } // sol #endif // SOL_STACK_HPP