// 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_PUSH_HPP #define SOL_STACK_PUSH_HPP #include "stack_core.hpp" #include "raii.hpp" #include "optional.hpp" #include namespace sol { namespace stack { template struct pusher { template static int push_keyed(lua_State* L, K&& k, Args&&... args) { // Basically, we store all user-data 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** pointerpointer = static_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); T*& referencereference = *pointerpointer; T* allocationtarget = reinterpret_cast(pointerpointer + 1); referencereference = allocationtarget; std::allocator alloc{}; alloc.construct(allocationtarget, std::forward(args)...); luaL_newmetatable(L, &k[0]); lua_setmetatable(L, -2); return 1; } template static int push(lua_State* L, Args&&... args) { return push_keyed(L, usertype_traits::metatable, std::forward(args)...); } }; template struct pusher { template static int push_keyed(lua_State* L, K&& k, T* obj) { if (obj == nullptr) return stack::push(L, nil); T** pref = static_cast(lua_newuserdata(L, sizeof(T*))); *pref = obj; luaL_getmetatable(L, &k[0]); lua_setmetatable(L, -2); return 1; } static int push(lua_State* L, T* obj) { return push_keyed(L, usertype_traits::metatable, obj); } }; template <> struct pusher { template static int push(lua_State* L, T&& obj) { return stack::push(L, detail::ptr(obj)); } }; template struct pusher::value>> { typedef typename unique_usertype_traits::type P; typedef typename unique_usertype_traits::actual_type Real; template >> = meta::enabler> static int push(lua_State* L, Arg&& arg) { if (unique_usertype_traits::is_null(arg)) return stack::push(L, nil); return push_deep(L, std::forward(arg)); } template static int push(lua_State* L, Arg0&& arg0, Arg0&& arg1, Args&&... args) { return push_deep(L, std::forward(arg0), std::forward(arg1), std::forward(args)...); } template static int push_deep(lua_State* L, Args&&... args) { P** pref = static_cast(lua_newuserdata(L, sizeof(P*) + sizeof(detail::special_destruct_func) + sizeof(Real))); detail::special_destruct_func* fx = static_cast(static_cast(pref + 1)); Real* mem = static_cast(static_cast(fx + 1)); *fx = detail::special_destruct; detail::default_construct::construct(mem, std::forward(args)...); *pref = unique_usertype_traits::get(*mem); if (luaL_newmetatable(L, &usertype_traits>::metatable[0]) == 1) { set_field(L, "__gc", detail::unique_destruct

); } lua_setmetatable(L, -2); return 1; } }; template struct pusher> { static int push(lua_State* L, const std::reference_wrapper& t) { return stack::push(L, std::addressof(detail::deref(t.get()))); } }; 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::value>> { static int push(lua_State* L, const T& value) { if (std::is_same::value) { return stack::push(L, static_cast(value)); } return stack::push(L, static_cast>(value)); } }; 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::neg>, meta::neg, std::is_base_of>>>::value>> { static int push(lua_State* L, const T& cont) { lua_createtable(L, static_cast(cont.size()), 0); int tableindex = lua_gettop(L); unsigned index = 1; for(auto&& i : cont) { set_field(L, index++, i, tableindex); } return 1; } }; template struct pusher, meta::has_key_value_pair, meta::neg, std::is_base_of>>>::value>> { static int push(lua_State* L, const T& cont) { lua_createtable(L, static_cast(cont.size()), 0); int tableindex = lua_gettop(L); for(auto&& pair : cont) { set_field(L, pair.first, pair.second, tableindex); } return 1; } }; template struct pusher::value || std::is_base_of::value>> { static int push(lua_State*, T& ref) { return ref.push(); } static int push(lua_State*, T&& ref) { return ref.push(); } }; 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, metatable_key_t) { lua_pushlstring(L, "__mt", 4); 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 cc) { lua_pushcclosure(L, cc.c_function, cc.upvalues); return 1; } }; template struct pusher> { template static int push(std::index_sequence, lua_State* L, T&& c) { int pushcount = multi_push(L, detail::forward_get(c.upvalues)...); return stack::push(L, c_closure(c.c_function, pushcount)); } template static int push(lua_State* L, T&& c) { return push(std::make_index_sequence<1 + sizeof...(Args)>(), L, std::forward(c)); } }; 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, light_userdata_value userdata) { lua_pushlightuserdata(L, userdata); return 1; } }; template struct pusher> { static int push(lua_State* L, light l) { lua_pushlightuserdata(L, static_cast(l.value)); return 1; } }; template struct pusher> { template static int push(lua_State* L, Args&&... args ) { // A dumb pusher void* rawdata = lua_newuserdata(L, sizeof(T)); std::allocator alloc; alloc.construct(static_cast(rawdata), std::forward(args)...); lua_CFunction cdel = stack_detail::alloc_destroy; // Make sure we have a plain GC set for this data lua_createtable(L, 0, 1); lua_pushlightuserdata(L, rawdata); lua_pushcclosure(L, cdel, 1); lua_setfield(L, -2, "__gc"); lua_setmetatable(L, -2); return 1; } }; template<> struct pusher { static int push(lua_State* L, userdata_value 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, char c) { const char str[2] = { c, '\0'}; return stack::push(L, str); } }; 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 { static int push(lua_State* L, meta_function m) { const std::string& str = name_of(m); lua_pushlstring(L, str.c_str(), str.size()); return 1; } }; #if 0 template<> struct pusher { static int push(lua_State* L, const wchar_t* wstr) { return push(L, wstr, wstr + std::char_traits::length(wstr)); } static int push(lua_State* L, const wchar_t* wstrb, const wchar_t* wstre) { std::string str{}; return stack::push(L, str); } }; template<> struct pusher { static int push(lua_State* L, const char16_t* u16str) { return push(L, u16str, u16str + std::char_traits::length(u16str)); } static int push(lua_State* L, const char16_t* u16strb, const char16_t* u16stre) { std::string str{}; return stack::push(L, str); } }; template<> struct pusher { static int push(lua_State* L, const char32_t* u32str) { return push(L, u32str, u32str + std::char_traits::length(u32str)); } static int push(lua_State* L, const char32_t* u32strb, const char32_t* u32stre) { std::string str{}; return stack::push(L, str); } }; template struct pusher { static int push(lua_State* L, const wchar_t(&str)[N]) { return stack::push(L, str, str + N - 1); } }; template struct pusher { static int push(lua_State* L, const char16_t(&str)[N]) { return stack::push(L, str, str + N - 1); } }; template struct pusher { static int push(lua_State* L, const char32_t(&str)[N]) { return stack::push(L, str, str + N - 1); } }; template <> struct pusher { static int push(lua_State* L, wchar_t c) { const wchar_t str[2] = { c, '\0' }; return stack::push(L, str); } }; template <> struct pusher { static int push(lua_State* L, char16_t c) { const char16_t str[2] = { c, '\0' }; return stack::push(L, str); } }; template <> struct pusher { static int push(lua_State* L, char32_t c) { const char32_t str[2] = { c, '\0' }; return stack::push(L, str); } }; template<> struct pusher { static int push(lua_State* L, const std::wstring& wstr) { return stack::push(L, wstr.data(), wstr.data() + wstr.size()); } }; template<> struct pusher { static int push(lua_State* L, const std::u16string& u16str) { return stack::push(L, u16str.data(), u16str.data() + u16str.size()); } }; template<> struct pusher { static int push(lua_State* L, const std::u32string& u32str) { return stack::push(L, u32str.data(), u32str.data() + u32str.size()); } }; #endif // Bad conversions template struct pusher> { template static int push(std::index_sequence, lua_State* L, T&& t) { int pushcount = 0; (void)detail::swallow{ 0, (pushcount += stack::push(L, detail::forward_get(t) ), 0)... }; return pushcount; } template static int push(lua_State* L, T&& t) { return push(std::index_sequence_for(), L, std::forward(t)); } }; template struct pusher> { template static int push(lua_State* L, T&& t) { int pushcount = stack::push(L, detail::forward_get<0>(t)); pushcount += stack::push(L, detail::forward_get<1>(t)); return pushcount; } }; template struct pusher> { template static int push(lua_State* L, T&& t) { if (t == nullopt) { return stack::push(L, nullopt); } return stack::push(L, t.value()); } }; template<> struct pusher { static int push(lua_State* L, nullopt_t) { return stack::push(L, nil); } }; template<> struct pusher { static int push(lua_State*, const this_state&) { return 0; } }; } // stack } // sol #endif // SOL_STACK_PUSH_HPP