// The MIT License (MIT) // Copyright (c) 2013 Danny Y., Rapptz // 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 "userdata_traits.hpp" #include #include #include #include namespace sol { namespace detail { template inline T* get_ptr(T& val) { return std::addressof(val); } template inline T* get_ptr(std::reference_wrapper val) { return std::addressof(val.get()); } template inline T* get_ptr(T* val) { return val; } template struct return_forward { typedef Unqualified T; T& operator()(T& value) const { return value; } T&& operator()(T&& value) const { return std::move(value); } T operator()(const T& value) const { return value; } // handle retarded libraries T operator()(const T&& value) const { return value; } }; } // detail namespace stack { namespace detail { template inline void push_userdata(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, std::addressof(metatablekey[0])); lua_setmetatable(L, -2); } } // detail template struct getter; template struct pusher; template struct getter { template> = 0> static U get(lua_State* L, int index = -1) { return lua_tonumber(L, index); } template, std::is_signed> = 0> static U get(lua_State* L, int index = -1) { return lua_tounsigned(L, index); } template, std::is_unsigned> = 0> static U get(lua_State* L, int index = -1) { return static_cast(lua_tointeger(L, index)); } template> = 0> static U get(lua_State* L, int index = -1) { return T(L, index); } template>, Not>, Not>> = 0> static U& get(lua_State* L, int index = -1) { void* udata = lua_touserdata(L, index); T* obj = static_cast(udata); return *obj; } }; template struct getter { static T* get(lua_State* L, int index = -1) { void* udata = lua_touserdata(L, index); T** obj = static_cast(udata); return *obj; } }; template struct getter { static T& get(lua_State* L, int index = -1) { void* udata = lua_touserdata(L, index); T** obj = static_cast(udata); return **obj; } }; 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::string::size_type 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* L, int index = -1) { if(lua_isnil(L, index) == 0) { throw sol::error("not nil"); } return nil_t{ }; } }; template<> struct getter { static userdata_t get(lua_State* L, int index = -1) { return{ lua_touserdata(L, index) }; } }; template<> struct getter { static lightuserdata_t get(lua_State* L, int index = 1) { return{ lua_touserdata(L, index) }; } }; template<> struct getter { static upvalue_t 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 pusher { template> = 0> static void push(lua_State* L, const T& value) { lua_pushnumber(L, value); } template, std::is_signed> = 0> static void push(lua_State* L, const T& value) { lua_pushinteger(L, value); } template, std::is_unsigned> = 0> static void push(lua_State* L, const T& value) { lua_pushunsigned(L, value); } template, Not>> = 0> static void push(lua_State* L, const T& cont) { lua_createtable(L, 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); } } template, has_key_value_pair> = 0> static void push(lua_State* L, const T& cont) { lua_createtable(L, cont.size(), 0); for(auto&& pair : cont) { pusher>{}.push(L, pair.first); pusher>{}.push(L, pair.second); lua_settable(L, -3); } } template> = 0> static void push(lua_State*, T& ref) { ref.push(); } template, EnableIf>, Not>, Not>, Not>> = 0> static void push(lua_State* L, T& t) { detail::push_userdata(L, userdata_traits::metatable, t); } template, EnableIf>, Not>, Not>, Not>> = 0> static void push(lua_State* L, T&& t) { detail::push_userdata(L, userdata_traits::metatable, std::move(t)); } }; template struct pusher { static void push(lua_State* L, T* obj) { detail::push_userdata(L, userdata_traits::metatable, obj); } }; template struct pusher> { static void push(lua_State* L, const std::reference_wrapper& t) { pusher{}.push(L, std::addressof(t.get())); } }; template<> struct pusher { static void push(lua_State* L, const bool& b) { lua_pushboolean(L, b); } }; template<> struct pusher { static void push(lua_State* L, const nil_t&) { lua_pushnil(L); } }; template<> struct pusher { static void push(lua_State* L, lua_CFunction func, int n = 0) { lua_pushcclosure(L, func, n); } }; template<> struct pusher { static void push(lua_State* L, void* userdata) { lua_pushlightuserdata(L, userdata); } }; template<> struct pusher { static void push(lua_State* L, upvalue_t upvalue) { lua_pushlightuserdata(L, upvalue); } }; template<> struct pusher { static void push(lua_State* L, lightuserdata_t userdata) { lua_pushlightuserdata(L, userdata); } }; template<> struct pusher { template> static void push(lua_State* L, T&& data) { U* userdata = static_cast(lua_newuserdata(L, sizeof(U))); new(userdata)U(std::forward(data)); } }; template<> struct pusher { static void push(lua_State* L, const char* str) { lua_pushlstring(L, str, std::char_traits::length(str)); } }; template struct pusher { static void push(lua_State* L, const char (&str)[N]) { lua_pushlstring(L, str, N - 1); } }; template<> struct pusher { static void push(lua_State* L, const std::string& str) { lua_pushlstring(L, str.c_str(), str.size()); } }; template inline void push(lua_State* L, T&& t, Args&&... args) { 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 void push(lua_State* L, Arg&& arg, Args&&... args) { pusher>{}.push(L, std::forward(arg), std::forward(args)...); } inline void push_args(lua_State*) { // do nothing } template inline void push_args(lua_State* L, T&& t, Args&&... args) { pusher>{}.push(L, std::forward(t)); using swallow = char[]; void(swallow{'\0', (pusher>{}.push(L, std::forward(args)), '\0')... }); } template> inline auto get(lua_State* L, int index = -1) -> decltype(getter{}.get(L, index)) { return getter{}.get(L, index); } template auto pop(lua_State* L) -> decltype(get(L)) { typedef decltype(get(L)) ret_t; ret_t r = get(L); lua_pop(L, 1); return r; } namespace detail { template inline int push_as_upvalues(lua_State* L, T& item) { typedef typename std::decay::type 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(std::addressof(data[0]), std::addressof(item), itemsize); for(auto&& v : data) { push(L, upvalue_t(v)); } return data_t_count; } 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 inline void push_tuple(lua_State* L, indices, T&& tuplen) { using swallow = char[1 + sizeof...(I)]; swallow {'\0', (sol::stack::push(L, std::get(tuplen)), '\0')... }; } template inline auto ltr_get(lua_State*, int, F&& f, types, types<>, Vs&&... vs) -> decltype(f(std::forward(vs)...)) { return f(std::forward(vs)...); } template inline auto ltr_get(lua_State* L, int index, F&& f, types, types, Vs&&... vs) -> decltype(f(std::declval()...)) { return ltr_get(L, index + 1, std::forward(f), types(), types(), std::forward(vs)..., stack::get(L, index)); } template inline auto ltr_pop(lua_State*, F&& f, types, types<>, Vs&&... vs) -> decltype(f(std::forward(vs)...)) { return f(std::forward(vs)...); } template inline auto ltr_pop(lua_State* L, F&& f, types t, types, Vs&&... vs) -> decltype(f(std::declval()...)) { return ltr_pop(L, std::forward(f), t, types(), std::forward(vs)..., stack::pop(L)); } template inline auto rtl_pop(lua_State*, F&& f, types, types<>, Vs&&... vs) -> decltype(f(std::forward(vs)...)) { return f(std::forward(vs)...); } template inline auto rtl_pop(lua_State* L, F&& f, types t, types, Vs&&... vs) -> decltype(f(std::declval()...)) { return rtl_pop(L, std::forward(f), t, types(), stack::pop(L), std::forward(vs)...); } } // detail template struct pusher> { static void push(lua_State* L, const std::tuple& tuplen) { detail::push_tuple(L, build_indices(), tuplen); } static void push(lua_State* L, std::tuple&& tuplen) { detail::push_tuple(L, build_indices(), std::move(tuplen)); } }; template inline void push_reverse(lua_State* L, T&& item) { push(L, std::forward(item)); } template inline void push_reverse(lua_State* L, const std::tuple& tuplen) { detail::push_tuple(L, build_reverse_indices(), tuplen); } template inline void push_reverse(lua_State* L, std::tuple&& tuplen) { detail::push_tuple(L, build_reverse_indices(), std::move(tuplen)); } template inline auto get_call(lua_State* L, int index, TFx&& fx, types t, Vs&&... vs) -> decltype(detail::ltr_get(L, index, std::forward(fx), t, t, std::forward(vs)...)) { return detail::ltr_get(L, index, std::forward(fx), t, t, std::forward(vs)...); } template inline auto get_call(lua_State* L, TFx&& fx, types t, Vs&&... vs) -> decltype(get_call(L, 1, std::forward(fx), t, std::forward(vs)...)) { return get_call(L, 1, std::forward(fx), t, std::forward(vs)...); } template inline auto pop_call(lua_State* L, TFx&& fx, types t) -> decltype(detail::ltr_pop(L, std::forward(fx), t, t)) { return detail::ltr_pop(L, std::forward(fx), t, t); } template inline auto pop_reverse_call(lua_State* L, TFx&& fx, types t) -> decltype(detail::rtl_pop(L, std::forward(fx), t, reversed())) { return detail::rtl_pop(L, std::forward(fx), t, reversed()); } inline call_syntax get_call_syntax(lua_State* L, const std::string& meta) { if(get(L, 1) == type::table) { if(luaL_newmetatable(L, meta.c_str()) == 0) { lua_settop(L, -2); return call_syntax::colon; } } return call_syntax::dot; } template struct get_return { typedef decltype(get(nullptr)) type; }; } // stack } // sol #endif // SOL_STACK_HPP