2016-03-25 03:45:44 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2017-05-15 22:41:50 +08:00
|
|
|
// Copyright (c) 2013-2017 Rapptz, ThePhD and contributors
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
// 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"
|
2016-03-31 04:52:51 +08:00
|
|
|
#include "optional.hpp"
|
2016-03-25 03:45:44 +08:00
|
|
|
#include <memory>
|
2016-08-13 01:08:59 +08:00
|
|
|
#ifdef SOL_CODECVT_SUPPORT
|
2016-08-12 23:06:14 +08:00
|
|
|
#include <codecvt>
|
2016-08-13 00:30:18 +08:00
|
|
|
#include <locale>
|
2016-08-13 01:08:59 +08:00
|
|
|
#endif
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-06-20 05:59:40 +08:00
|
|
|
namespace stack {
|
2017-04-19 08:23:20 +08:00
|
|
|
inline int push_environment_of(lua_State* L, int index = -1) {
|
|
|
|
#if SOL_LUA_VERSION < 502
|
|
|
|
// Use lua_setfenv
|
|
|
|
lua_getfenv(L, index);
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
// Use upvalues as explained in Lua 5.2 and beyond's manual
|
|
|
|
if (lua_getupvalue(L, index, 1) == nullptr) {
|
|
|
|
push(L, lua_nil);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int push_environment_of(const T& target) {
|
|
|
|
target.push();
|
|
|
|
return push_environment_of(target.lua_state(), -1) + 1;
|
|
|
|
}
|
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename T>
|
|
|
|
struct pusher<detail::as_value_tag<T>> {
|
|
|
|
template <typename F, typename... Args>
|
|
|
|
static int push_fx(lua_State* L, F&& f, Args&&... args) {
|
2016-06-20 05:59:40 +08:00
|
|
|
// 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<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
|
|
|
T*& referencereference = *pointerpointer;
|
|
|
|
T* allocationtarget = reinterpret_cast<T*>(pointerpointer + 1);
|
|
|
|
referencereference = allocationtarget;
|
|
|
|
std::allocator<T> alloc{};
|
|
|
|
alloc.construct(allocationtarget, std::forward<Args>(args)...);
|
2016-08-24 09:42:27 +08:00
|
|
|
f();
|
2016-06-20 05:59:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename K, typename... Args>
|
|
|
|
static int push_keyed(lua_State* L, K&& k, Args&&... args) {
|
|
|
|
return push_fx(L, [&L, &k]() {
|
|
|
|
luaL_newmetatable(L, &k[0]);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template <typename... Args>
|
|
|
|
static int push(lua_State* L, Args&&... args) {
|
2016-10-22 05:32:27 +08:00
|
|
|
return push_keyed(L, usertype_traits<T>::metatable(), std::forward<Args>(args)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename T>
|
|
|
|
struct pusher<detail::as_pointer_tag<T>> {
|
|
|
|
template <typename F>
|
|
|
|
static int push_fx(lua_State* L, F&& f, T* obj) {
|
2016-06-20 05:59:40 +08:00
|
|
|
if (obj == nullptr)
|
2016-12-03 20:33:18 +08:00
|
|
|
return stack::push(L, lua_nil);
|
2016-06-20 05:59:40 +08:00
|
|
|
T** pref = static_cast<T**>(lua_newuserdata(L, sizeof(T*)));
|
|
|
|
*pref = obj;
|
2016-08-24 09:42:27 +08:00
|
|
|
f();
|
2016-06-20 05:59:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename K>
|
|
|
|
static int push_keyed(lua_State* L, K&& k, T* obj) {
|
|
|
|
return push_fx(L, [&L, &k]() {
|
|
|
|
luaL_newmetatable(L, &k[0]);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}, obj);
|
|
|
|
}
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
static int push(lua_State* L, T* obj) {
|
2016-10-22 05:32:27 +08:00
|
|
|
return push_keyed(L, usertype_traits<meta::unqualified_t<T>*>::metatable(), obj);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct pusher<detail::as_reference_tag> {
|
|
|
|
template <typename T>
|
|
|
|
static int push(lua_State* L, T&& obj) {
|
|
|
|
return stack::push(L, detail::ptr(obj));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
template<typename T, typename>
|
|
|
|
struct pusher {
|
|
|
|
template <typename... Args>
|
|
|
|
static int push(lua_State* L, Args&&... args) {
|
|
|
|
return pusher<detail::as_value_tag<T>>{}.push(L, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-11-30 01:51:21 +08:00
|
|
|
struct pusher<T*, meta::disable_if_t<meta::all<is_container<meta::unqualified_t<T>>, meta::neg<meta::any<std::is_base_of<reference, meta::unqualified_t<T>>, std::is_base_of<stack_reference, meta::unqualified_t<T>>>>>::value>> {
|
2016-08-24 09:42:27 +08:00
|
|
|
template <typename... Args>
|
|
|
|
static int push(lua_State* L, Args&&... args) {
|
|
|
|
return pusher<detail::as_pointer_tag<T>>{}.push(L, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<is_unique_usertype<T>::value>> {
|
|
|
|
typedef typename unique_usertype_traits<T>::type P;
|
|
|
|
typedef typename unique_usertype_traits<T>::actual_type Real;
|
|
|
|
|
|
|
|
template <typename Arg, meta::enable<std::is_base_of<Real, meta::unqualified_t<Arg>>> = meta::enabler>
|
|
|
|
static int push(lua_State* L, Arg&& arg) {
|
|
|
|
return push_deep(L, std::forward<Arg>(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Arg0, typename Arg1, typename... Args>
|
|
|
|
static int push(lua_State* L, Arg0&& arg0, Arg0&& arg1, Args&&... args) {
|
|
|
|
return push_deep(L, std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
static int push_deep(lua_State* L, Args&&... args) {
|
2017-06-15 13:23:51 +08:00
|
|
|
P** pref = static_cast<P**>(lua_newuserdata(L, sizeof(P*) + sizeof(detail::unique_destructor) + sizeof(Real)));
|
|
|
|
detail::unique_destructor* fx = static_cast<detail::unique_destructor*>(static_cast<void*>(pref + 1));
|
2016-06-20 05:59:40 +08:00
|
|
|
Real* mem = static_cast<Real*>(static_cast<void*>(fx + 1));
|
2017-06-15 13:23:51 +08:00
|
|
|
*fx = detail::usertype_unique_alloc_destroy<P, Real>;
|
2016-06-20 05:59:40 +08:00
|
|
|
detail::default_construct::construct(mem, std::forward<Args>(args)...);
|
|
|
|
*pref = unique_usertype_traits<T>::get(*mem);
|
2016-10-22 05:32:27 +08:00
|
|
|
if (luaL_newmetatable(L, &usertype_traits<detail::unique_usertype<P>>::metatable()[0]) == 1) {
|
2016-06-20 05:59:40 +08:00
|
|
|
set_field(L, "__gc", detail::unique_destruct<P>);
|
|
|
|
}
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<std::reference_wrapper<T>> {
|
|
|
|
static int push(lua_State* L, const std::reference_wrapper<T>& t) {
|
|
|
|
return stack::push(L, std::addressof(detail::deref(t.get())));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<std::is_floating_point<T>::value>> {
|
|
|
|
static int push(lua_State* L, const T& value) {
|
|
|
|
lua_pushnumber(L, value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<meta::all<std::is_integral<T>, std::is_signed<T>>::value>> {
|
|
|
|
static int push(lua_State* L, const T& value) {
|
|
|
|
lua_pushinteger(L, static_cast<lua_Integer>(value));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<std::is_enum<T>::value>> {
|
|
|
|
static int push(lua_State* L, const T& value) {
|
|
|
|
if (std::is_same<char, T>::value) {
|
|
|
|
return stack::push(L, static_cast<int>(value));
|
|
|
|
}
|
|
|
|
return stack::push(L, static_cast<std::underlying_type_t<T>>(value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<meta::all<std::is_integral<T>, std::is_unsigned<T>>::value>> {
|
|
|
|
static int push(lua_State* L, const T& value) {
|
2016-06-22 13:20:38 +08:00
|
|
|
lua_pushinteger(L, static_cast<lua_Integer>(value));
|
|
|
|
return 1;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-08-24 09:42:27 +08:00
|
|
|
struct pusher<as_table_t<T>, std::enable_if_t<!meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>>::value>> {
|
|
|
|
static int push(lua_State* L, const as_table_t<T>& tablecont) {
|
|
|
|
auto& cont = detail::deref(detail::unwrap(tablecont.source));
|
2016-06-20 05:59:40 +08:00
|
|
|
lua_createtable(L, static_cast<int>(cont.size()), 0);
|
|
|
|
int tableindex = lua_gettop(L);
|
2016-08-24 09:42:27 +08:00
|
|
|
std::size_t index = 1;
|
|
|
|
for (const auto& i : cont) {
|
|
|
|
#if SOL_LUA_VERSION >= 503
|
|
|
|
int p = stack::push(L, i);
|
|
|
|
for (int pi = 0; pi < p; ++pi) {
|
|
|
|
lua_seti(L, tableindex, static_cast<lua_Integer>(index++));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
lua_pushinteger(L, static_cast<lua_Integer>(index));
|
|
|
|
int p = stack::push(L, i);
|
|
|
|
if (p == 1) {
|
|
|
|
++index;
|
|
|
|
lua_settable(L, tableindex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int firstindex = tableindex + 1 + 1;
|
|
|
|
for (int pi = 0; pi < p; ++pi) {
|
|
|
|
stack::push(L, index);
|
|
|
|
lua_pushvalue(L, firstindex);
|
|
|
|
lua_settable(L, tableindex);
|
|
|
|
++index;
|
|
|
|
++firstindex;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1 + p);
|
|
|
|
}
|
|
|
|
#endif
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2016-10-06 08:55:40 +08:00
|
|
|
// TODO: figure out a better way to do this...?
|
|
|
|
//set_field(L, -1, cont.size());
|
2016-06-20 05:59:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-08-24 09:42:27 +08:00
|
|
|
struct pusher<as_table_t<T>, std::enable_if_t<meta::has_key_value_pair<meta::unqualified_t<std::remove_pointer_t<T>>>::value>> {
|
|
|
|
static int push(lua_State* L, const as_table_t<T>& tablecont) {
|
|
|
|
auto& cont = detail::deref(detail::unwrap(tablecont.source));
|
2016-06-20 05:59:40 +08:00
|
|
|
lua_createtable(L, static_cast<int>(cont.size()), 0);
|
|
|
|
int tableindex = lua_gettop(L);
|
2016-08-24 09:42:27 +08:00
|
|
|
for (const auto& pair : cont) {
|
2016-06-20 05:59:40 +08:00
|
|
|
set_field(L, pair.first, pair.second, tableindex);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<std::is_base_of<reference, T>::value || std::is_base_of<stack_reference, T>::value>> {
|
2016-12-06 23:51:14 +08:00
|
|
|
static int push(lua_State* L, const T& ref) {
|
|
|
|
return ref.push(L);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-06 23:51:14 +08:00
|
|
|
static int push(lua_State* L, T&& ref) {
|
|
|
|
return ref.push(L);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<bool> {
|
|
|
|
static int push(lua_State* L, bool b) {
|
|
|
|
lua_pushboolean(L, b);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2016-12-03 20:33:18 +08:00
|
|
|
struct pusher<lua_nil_t> {
|
|
|
|
static int push(lua_State* L, lua_nil_t) {
|
2016-06-20 05:59:40 +08:00
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2017-04-19 08:23:20 +08:00
|
|
|
struct pusher<metatable_t> {
|
|
|
|
static int push(lua_State* L, metatable_t) {
|
2016-06-20 05:59:40 +08:00
|
|
|
lua_pushlstring(L, "__mt", 4);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<std::remove_pointer_t<lua_CFunction>> {
|
|
|
|
static int push(lua_State* L, lua_CFunction func, int n = 0) {
|
|
|
|
lua_pushcclosure(L, func, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<lua_CFunction> {
|
|
|
|
static int push(lua_State* L, lua_CFunction func, int n = 0) {
|
|
|
|
lua_pushcclosure(L, func, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<c_closure> {
|
|
|
|
static int push(lua_State* L, c_closure cc) {
|
|
|
|
lua_pushcclosure(L, cc.c_function, cc.upvalues);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Arg, typename... Args>
|
|
|
|
struct pusher<closure<Arg, Args...>> {
|
|
|
|
template <std::size_t... I, typename T>
|
|
|
|
static int push(std::index_sequence<I...>, lua_State* L, T&& c) {
|
|
|
|
int pushcount = multi_push(L, detail::forward_get<I>(c.upvalues)...);
|
|
|
|
return stack::push(L, c_closure(c.c_function, pushcount));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static int push(lua_State* L, T&& c) {
|
|
|
|
return push(std::make_index_sequence<1 + sizeof...(Args)>(), L, std::forward<T>(c));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<void*> {
|
|
|
|
static int push(lua_State* L, void* userdata) {
|
|
|
|
lua_pushlightuserdata(L, userdata);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<lightuserdata_value> {
|
|
|
|
static int push(lua_State* L, lightuserdata_value userdata) {
|
|
|
|
lua_pushlightuserdata(L, userdata);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<light<T>> {
|
|
|
|
static int push(lua_State* L, light<T> l) {
|
|
|
|
lua_pushlightuserdata(L, static_cast<void*>(l.value));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<user<T>> {
|
2016-08-23 10:45:06 +08:00
|
|
|
template <bool with_meta = true, typename Key, typename... Args>
|
|
|
|
static int push_with(lua_State* L, Key&& name, Args&&... args) {
|
2016-06-20 05:59:40 +08:00
|
|
|
// A dumb pusher
|
|
|
|
void* rawdata = lua_newuserdata(L, sizeof(T));
|
2016-07-08 04:52:39 +08:00
|
|
|
T* data = static_cast<T*>(rawdata);
|
2016-06-20 05:59:40 +08:00
|
|
|
std::allocator<T> alloc;
|
2016-07-08 04:52:39 +08:00
|
|
|
alloc.construct(data, std::forward<Args>(args)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
if (with_meta) {
|
2016-08-24 09:42:27 +08:00
|
|
|
lua_CFunction cdel = detail::user_alloc_destroy<T>;
|
2016-06-20 05:59:40 +08:00
|
|
|
// Make sure we have a plain GC set for this data
|
2016-07-10 01:38:55 +08:00
|
|
|
if (luaL_newmetatable(L, name) != 0) {
|
2016-11-23 15:39:52 +08:00
|
|
|
lua_pushcclosure(L, cdel, 0);
|
2016-07-10 01:38:55 +08:00
|
|
|
lua_setfield(L, -2, "__gc");
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-19 08:23:20 +08:00
|
|
|
template <typename Arg, typename... Args, meta::disable<meta::any_same<meta::unqualified_t<Arg>, no_metatable_t, metatable_t>> = meta::enabler>
|
2016-07-13 08:55:26 +08:00
|
|
|
static int push(lua_State* L, Arg&& arg, Args&&... args) {
|
2016-10-22 05:32:27 +08:00
|
|
|
const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
|
2016-08-23 10:45:06 +08:00
|
|
|
return push_with(L, name, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
2016-07-13 08:55:26 +08:00
|
|
|
}
|
|
|
|
|
2016-07-08 04:52:39 +08:00
|
|
|
template <typename... Args>
|
2016-07-13 08:55:26 +08:00
|
|
|
static int push(lua_State* L, no_metatable_t, Args&&... args) {
|
2016-10-22 05:32:27 +08:00
|
|
|
const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
|
2016-08-23 10:45:06 +08:00
|
|
|
return push_with<false>(L, name, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key, typename... Args>
|
2017-04-19 08:23:20 +08:00
|
|
|
static int push(lua_State* L, metatable_t, Key&& key, Args&&... args) {
|
2016-08-23 10:45:06 +08:00
|
|
|
const auto name = &key[0];
|
|
|
|
return push_with<true>(L, name, std::forward<Args>(args)...);
|
2016-07-08 04:52:39 +08:00
|
|
|
}
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
static int push(lua_State* L, const user<T>& u) {
|
2016-10-22 05:32:27 +08:00
|
|
|
const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
|
2016-08-23 10:45:06 +08:00
|
|
|
return push_with(L, name, u.value);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, user<T>&& u) {
|
2016-10-22 05:32:27 +08:00
|
|
|
const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
|
2016-08-23 10:45:06 +08:00
|
|
|
return push_with(L, name, std::move(u.value));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, no_metatable_t, const user<T>& u) {
|
2016-10-22 05:32:27 +08:00
|
|
|
const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
|
2016-08-23 10:45:06 +08:00
|
|
|
return push_with<false>(L, name, u.value);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, no_metatable_t, user<T>&& u) {
|
2016-10-22 05:32:27 +08:00
|
|
|
const auto name = &usertype_traits<meta::unqualified_t<T>>::user_gc_metatable()[0];
|
2016-08-23 10:45:06 +08:00
|
|
|
return push_with<false>(L, name, std::move(u.value));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<userdata_value> {
|
|
|
|
static int push(lua_State* L, userdata_value data) {
|
|
|
|
void** ud = static_cast<void**>(lua_newuserdata(L, sizeof(void*)));
|
|
|
|
*ud = data.value;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<const char*> {
|
2016-08-11 19:25:57 +08:00
|
|
|
static int push_sized(lua_State* L, const char* str, std::size_t len) {
|
|
|
|
lua_pushlstring(L, str, len);
|
2016-06-20 05:59:40 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-08-12 23:06:14 +08:00
|
|
|
|
2016-08-11 19:25:57 +08:00
|
|
|
static int push(lua_State* L, const char* str) {
|
2016-12-02 18:47:24 +08:00
|
|
|
if (str == nullptr)
|
2016-12-03 20:33:18 +08:00
|
|
|
return stack::push(L, lua_nil);
|
2016-08-11 19:25:57 +08:00
|
|
|
return push_sized(L, str, std::char_traits<char>::length(str));
|
|
|
|
}
|
|
|
|
|
2016-08-12 23:06:14 +08:00
|
|
|
static int push(lua_State* L, const char* strb, const char* stre) {
|
|
|
|
return push_sized(L, strb, stre - strb);
|
|
|
|
}
|
|
|
|
|
2016-08-11 19:25:57 +08:00
|
|
|
static int push(lua_State* L, const char* str, std::size_t len) {
|
|
|
|
return push_sized(L, str, len);
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
struct pusher<char[N]> {
|
|
|
|
static int push(lua_State* L, const char(&str)[N]) {
|
|
|
|
lua_pushlstring(L, str, N - 1);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-11 21:16:23 +08:00
|
|
|
|
|
|
|
static int push(lua_State* L, const char(&str)[N], std::size_t sz) {
|
|
|
|
lua_pushlstring(L, str, sz);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct pusher<char> {
|
|
|
|
static int push(lua_State* L, char c) {
|
|
|
|
const char str[2] = { c, '\0' };
|
2016-08-11 21:16:23 +08:00
|
|
|
return stack::push(L, str, 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<std::string> {
|
|
|
|
static int push(lua_State* L, const std::string& str) {
|
|
|
|
lua_pushlstring(L, str.c_str(), str.size());
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-11 21:16:23 +08:00
|
|
|
|
|
|
|
static int push(lua_State* L, const std::string& str, std::size_t sz) {
|
|
|
|
lua_pushlstring(L, str.c_str(), sz);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<meta_function> {
|
|
|
|
static int push(lua_State* L, meta_function m) {
|
2017-04-05 04:16:22 +08:00
|
|
|
const std::string& str = to_string(m);
|
2016-06-20 05:59:40 +08:00
|
|
|
lua_pushlstring(L, str.c_str(), str.size());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
2016-06-04 09:40:23 +08:00
|
|
|
|
2016-08-13 01:08:59 +08:00
|
|
|
#ifdef SOL_CODECVT_SUPPORT
|
2016-06-20 05:59:40 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<const wchar_t*> {
|
|
|
|
static int push(lua_State* L, const wchar_t* wstr) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, wstr, std::char_traits<wchar_t>::length(wstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const wchar_t* wstr, std::size_t sz) {
|
|
|
|
return push(L, wstr, wstr + sz);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2016-08-12 23:06:14 +08:00
|
|
|
|
|
|
|
static int push(lua_State* L, const wchar_t* strb, const wchar_t* stre) {
|
2016-08-13 00:30:18 +08:00
|
|
|
if (sizeof(wchar_t) == 2) {
|
2017-01-31 10:40:58 +08:00
|
|
|
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
|
2016-08-13 00:30:18 +08:00
|
|
|
std::string u8str = convert.to_bytes(strb, stre);
|
|
|
|
return stack::push(L, u8str);
|
|
|
|
}
|
2017-01-31 10:40:58 +08:00
|
|
|
static std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
|
2016-08-13 00:30:18 +08:00
|
|
|
std::string u8str = convert.to_bytes(strb, stre);
|
2016-08-12 23:06:14 +08:00
|
|
|
return stack::push(L, u8str);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<const char16_t*> {
|
|
|
|
static int push(lua_State* L, const char16_t* u16str) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, u16str, std::char_traits<char16_t>::length(u16str));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2016-08-12 23:06:14 +08:00
|
|
|
|
|
|
|
static int push(lua_State* L, const char16_t* u16str, std::size_t sz) {
|
|
|
|
return push(L, u16str, u16str + sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const char16_t* strb, const char16_t* stre) {
|
|
|
|
#ifdef _MSC_VER
|
2017-01-31 10:40:58 +08:00
|
|
|
static std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> convert;
|
2016-08-12 23:06:14 +08:00
|
|
|
std::string u8str = convert.to_bytes(reinterpret_cast<const int16_t*>(strb), reinterpret_cast<const int16_t*>(stre));
|
|
|
|
#else
|
2017-01-31 10:40:58 +08:00
|
|
|
static std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
2016-08-12 23:06:14 +08:00
|
|
|
std::string u8str = convert.to_bytes(strb, stre);
|
|
|
|
#endif // VC++ is a shit
|
|
|
|
return stack::push(L, u8str);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<const char32_t*> {
|
|
|
|
static int push(lua_State* L, const char32_t* u32str) {
|
|
|
|
return push(L, u32str, u32str + std::char_traits<char32_t>::length(u32str));
|
|
|
|
}
|
2016-08-12 23:06:14 +08:00
|
|
|
|
|
|
|
static int push(lua_State* L, const char32_t* u32str, std::size_t sz) {
|
|
|
|
return push(L, u32str, u32str + sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const char32_t* strb, const char32_t* stre) {
|
|
|
|
#ifdef _MSC_VER
|
2017-01-31 10:40:58 +08:00
|
|
|
static std::wstring_convert<std::codecvt_utf8<int32_t>, int32_t> convert;
|
2016-08-12 23:06:14 +08:00
|
|
|
std::string u8str = convert.to_bytes(reinterpret_cast<const int32_t*>(strb), reinterpret_cast<const int32_t*>(stre));
|
|
|
|
#else
|
2017-01-31 10:40:58 +08:00
|
|
|
static std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
|
2016-08-12 23:06:14 +08:00
|
|
|
std::string u8str = convert.to_bytes(strb, stre);
|
|
|
|
#endif // VC++ is a shit
|
|
|
|
return stack::push(L, u8str);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
struct pusher<wchar_t[N]> {
|
|
|
|
static int push(lua_State* L, const wchar_t(&str)[N]) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, str, N - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const wchar_t(&str)[N], std::size_t sz) {
|
|
|
|
return stack::push<const wchar_t*>(L, str, str + sz);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
struct pusher<char16_t[N]> {
|
|
|
|
static int push(lua_State* L, const char16_t(&str)[N]) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, str, N - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const char16_t(&str)[N], std::size_t sz) {
|
|
|
|
return stack::push<const char16_t*>(L, str, str + sz);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
struct pusher<char32_t[N]> {
|
|
|
|
static int push(lua_State* L, const char32_t(&str)[N]) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, str, N - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const char32_t(&str)[N], std::size_t sz) {
|
|
|
|
return stack::push<const char32_t*>(L, str, str + sz);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct pusher<wchar_t> {
|
|
|
|
static int push(lua_State* L, wchar_t c) {
|
|
|
|
const wchar_t str[2] = { c, '\0' };
|
2016-08-12 23:06:14 +08:00
|
|
|
return stack::push(L, str, 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct pusher<char16_t> {
|
|
|
|
static int push(lua_State* L, char16_t c) {
|
|
|
|
const char16_t str[2] = { c, '\0' };
|
2016-08-12 23:06:14 +08:00
|
|
|
return stack::push(L, str, 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct pusher<char32_t> {
|
|
|
|
static int push(lua_State* L, char32_t c) {
|
|
|
|
const char32_t str[2] = { c, '\0' };
|
2016-08-12 23:06:14 +08:00
|
|
|
return stack::push(L, str, 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<std::wstring> {
|
|
|
|
static int push(lua_State* L, const std::wstring& wstr) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, wstr.data(), wstr.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const std::wstring& wstr, std::size_t sz) {
|
|
|
|
return stack::push(L, wstr.data(), wstr.data() + sz);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<std::u16string> {
|
|
|
|
static int push(lua_State* L, const std::u16string& u16str) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, u16str, u16str.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, const std::u16string& u16str, std::size_t sz) {
|
|
|
|
return stack::push(L, u16str.data(), u16str.data() + sz);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<std::u32string> {
|
|
|
|
static int push(lua_State* L, const std::u32string& u32str) {
|
2016-08-12 23:06:14 +08:00
|
|
|
return push(L, u32str, u32str.size());
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2016-05-19 09:29:17 +08:00
|
|
|
|
2016-08-12 23:06:14 +08:00
|
|
|
static int push(lua_State* L, const std::u32string& u32str, std::size_t sz) {
|
|
|
|
return stack::push(L, u32str.data(), u32str.data() + sz);
|
|
|
|
}
|
|
|
|
};
|
2016-08-13 03:57:53 +08:00
|
|
|
#endif // codecvt Header Support
|
2016-05-19 14:15:42 +08:00
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template<typename... Args>
|
|
|
|
struct pusher<std::tuple<Args...>> {
|
|
|
|
template <std::size_t... I, typename T>
|
|
|
|
static int push(std::index_sequence<I...>, lua_State* L, T&& t) {
|
|
|
|
int pushcount = 0;
|
|
|
|
(void)detail::swallow{ 0, (pushcount += stack::push(L,
|
|
|
|
detail::forward_get<I>(t)
|
|
|
|
), 0)... };
|
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static int push(lua_State* L, T&& t) {
|
|
|
|
return push(std::index_sequence_for<Args...>(), L, std::forward<T>(t));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A, typename B>
|
|
|
|
struct pusher<std::pair<A, B>> {
|
|
|
|
template <typename T>
|
|
|
|
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<typename O>
|
|
|
|
struct pusher<optional<O>> {
|
|
|
|
template <typename T>
|
|
|
|
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<nullopt_t> {
|
|
|
|
static int push(lua_State* L, nullopt_t) {
|
2016-12-03 20:33:18 +08:00
|
|
|
return stack::push(L, lua_nil);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-26 05:31:31 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<std::nullptr_t> {
|
|
|
|
static int push(lua_State* L, std::nullptr_t) {
|
2016-12-03 20:33:18 +08:00
|
|
|
return stack::push(L, lua_nil);
|
2016-07-26 05:31:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<this_state> {
|
|
|
|
static int push(lua_State*, const this_state&) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2017-04-03 10:37:01 +08:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct pusher<new_table> {
|
|
|
|
static int push(lua_State* L, const new_table& nt) {
|
|
|
|
lua_createtable(L, nt.sequence_hint, nt.map_hint);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
2016-06-20 05:59:40 +08:00
|
|
|
} // stack
|
2016-03-25 03:45:44 +08:00
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_STACK_PUSH_HPP
|