mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Merge branch 'master' of https://github.com/ThePhD/sol into ThePhD
This commit is contained in:
commit
9e47ec6b06
|
@ -27,6 +27,37 @@
|
|||
#include <memory>
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
template<typename T, std::size_t n>
|
||||
void get_upvalue_ptr( lua_State* L, T*& data, std::size_t datasize, std::array<void*, n> voiddata, int& upvalue ) {
|
||||
for ( std::size_t i = 0, d = 0; d < datasize; ++i, d += sizeof( void* ) ) {
|
||||
voiddata[ i ] = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
|
||||
}
|
||||
data = reinterpret_cast<T*>( static_cast<void*>( voiddata.data( ) ) );
|
||||
}
|
||||
template<typename T, std::size_t n>
|
||||
void get_upvalue_ptr( lua_State* L, T*& data, std::array<void*, n> voiddata, int& upvalue ) {
|
||||
get_upvalue_ptr( L, data, sizeof( T ), voiddata, upvalue );
|
||||
}
|
||||
template<typename T>
|
||||
void get_upvalue_ptr( lua_State* L, T*& data, int& upvalue ) {
|
||||
const static std::size_t data_t_count = ( sizeof(T)+( sizeof(void*)-1 ) ) / sizeof( void* );
|
||||
typedef std::array<void*, data_t_count> data_t;
|
||||
data_t voiddata{{}};
|
||||
return get_upvalue_ptr(L, data, voiddata, upvalue);
|
||||
}
|
||||
template<typename T>
|
||||
void get_upvalue( lua_State* L, T& data, int& upvalue ) {
|
||||
const static std::size_t data_t_count = ( sizeof(T)+( sizeof(void*)-1 ) ) / sizeof( void* );
|
||||
typedef std::array<void*, data_t_count> data_t;
|
||||
data_t voiddata{ { } };
|
||||
for ( std::size_t i = 0, d = 0; d < sizeof(T); ++i, d += sizeof( void* ) ) {
|
||||
voiddata[ i ] = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
|
||||
}
|
||||
data = *reinterpret_cast<T*>( static_cast<void*>( voiddata.data( ) ) );
|
||||
}
|
||||
} // detail
|
||||
|
||||
|
||||
template<typename TFx>
|
||||
struct static_lua_func {
|
||||
|
@ -47,10 +78,9 @@ struct static_lua_func {
|
|||
}
|
||||
|
||||
static int call(lua_State* L) {
|
||||
void* functiondata = lua_touserdata(L, lua_upvalueindex(1));
|
||||
//if (functiondata == nullptr)
|
||||
// throw sol_error("call from lua to c++ function has null function pointer data");
|
||||
fx_t* fx = reinterpret_cast<fx_t*>(functiondata);
|
||||
int upvalue = 1;
|
||||
fx_t* fx;
|
||||
detail::get_upvalue( L, fx, upvalue );
|
||||
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L);
|
||||
return r;
|
||||
}
|
||||
|
@ -74,7 +104,9 @@ struct static_object_lua_func {
|
|||
|
||||
template<typename TR, typename... Args>
|
||||
static int typed_call(types<TR>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
||||
auto fx = [ &item, &ifx ] (Args&&... args) -> TR { return (item.*ifx)(std::forward<Args>(args)...); };
|
||||
auto fx = [ &item, &ifx ] (Args&&... args) -> TR {
|
||||
return (item.*ifx)(std::forward<Args>(args)...);
|
||||
};
|
||||
auto r = stack::pop_call(L, fx, types<Args...>());
|
||||
stack::push(L, std::move(r));
|
||||
return 1;
|
||||
|
@ -91,16 +123,15 @@ struct static_object_lua_func {
|
|||
static int call(lua_State* L) {
|
||||
const static std::size_t data_t_count = ( sizeof(fx_t)+( sizeof(void*)-1 ) ) / sizeof( void* );
|
||||
typedef std::array<void*, data_t_count> data_t;
|
||||
data_t fxptrdata;
|
||||
int upvalue = 1;
|
||||
for (std::size_t i = 0; i < fxptrdata.size(); ++i) {
|
||||
fxptrdata[ i ] = lua_touserdata(L, lua_upvalueindex(upvalue++));
|
||||
data_t data = { { } };
|
||||
fx_t* fxptr;
|
||||
for ( std::size_t i = 0, d = 0; d < sizeof(fx_t*); ++i, d += sizeof( void* ) ) {
|
||||
data[ i ] = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
|
||||
}
|
||||
void* objectdata = lua_touserdata(L, lua_upvalueindex(upvalue++));
|
||||
//if (objectdata == nullptr)
|
||||
// throw sol_error("call from lua to c++ function has null object data");
|
||||
fx_t* fxptr = reinterpret_cast<fx_t*>(static_cast<void*>(fxptrdata.data()));
|
||||
fxptr = reinterpret_cast<fx_t*>( static_cast<void*>( data.data( ) ) );
|
||||
fx_t& mem_ptr = *fxptr;
|
||||
void* objectdata = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
|
||||
T& obj = *static_cast<T*>( objectdata );
|
||||
int r = typed_call( tuple_types<typename fx_traits::return_type>( ), typename fx_traits::args_type( ), obj, mem_ptr, L );
|
||||
return r;
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
|
||||
namespace sol {
|
||||
template<typename T, typename R = void>
|
||||
|
@ -183,6 +184,21 @@ inline void push(lua_State* L, const std::array<T, N>& data) {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline int push_user( lua_State* L, T& item ) {
|
||||
typedef typename std::decay<T>::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<void*, data_t_count> data_t;
|
||||
|
||||
data_t data{{}};
|
||||
std::memcpy( std::addressof( data[ 0 ] ), std::addressof( item ), itemsize );
|
||||
push( L, data );
|
||||
return data_t_count;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename T, std::size_t... I>
|
||||
inline void push(lua_State* L, indices<I...>, const T& tuplen) {
|
||||
|
|
|
@ -128,8 +128,6 @@ private:
|
|||
table& set_fx(std::true_type, T&& key, TFx&& fx, TObj&& obj) {
|
||||
typedef typename std::decay<TObj>::type decay_of_to;
|
||||
typedef typename std::decay<TFx>::type decay_of_tfx;
|
||||
const static std::size_t data_t_count = (sizeof(decay_of_tfx)+(sizeof(void*)-1)) / sizeof(void*);
|
||||
typedef std::array<void*, data_t_count> data_t;
|
||||
|
||||
std::string fkey(key);
|
||||
|
||||
|
@ -139,9 +137,6 @@ private:
|
|||
// We don't need to store the size, because the other side is templated
|
||||
// with the same member function pointer type
|
||||
decay_of_tfx fxptr(std::forward<TFx>(fx));
|
||||
data_t fxptrdata;
|
||||
std::size_t fxptrsize = sizeof(fxptr);
|
||||
std::memcpy(std::addressof(fxptrdata[ 0 ]), std::addressof(fxptr), fxptrsize);
|
||||
void* userobjdata = static_cast<void*>(detail::get_ptr(obj));
|
||||
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
|
||||
const char* freefuncname = fkey.c_str();
|
||||
|
@ -153,9 +148,9 @@ private:
|
|||
|
||||
push();
|
||||
|
||||
stack::push(state(), fxptrdata);
|
||||
int upvalues = stack::push_user(state(), fxptr);
|
||||
stack::push(state(), userobjdata);
|
||||
luaL_setfuncs(state(), funcreg, fxptrdata.size() + 1);
|
||||
luaL_setfuncs(state(), funcreg, upvalues + 1);
|
||||
|
||||
lua_pop(state(), 1);
|
||||
return *this;
|
||||
|
@ -166,7 +161,6 @@ private:
|
|||
typedef typename std::decay<TFx>::type ptr_fx;
|
||||
std::string fkey(key);
|
||||
ptr_fx target(std::forward<TFx>(fx));
|
||||
void* userdata = reinterpret_cast<void*>(target);
|
||||
lua_CFunction freefunc = &static_lua_func<TFx>::call;
|
||||
const char* freefuncname = fkey.c_str();
|
||||
const luaL_Reg funcreg[ 2 ] = {
|
||||
|
@ -176,8 +170,8 @@ private:
|
|||
|
||||
push();
|
||||
|
||||
stack::push(state(), userdata);
|
||||
luaL_setfuncs(state(), funcreg, 1);
|
||||
int upvalues = stack::push_user( state( ), target );
|
||||
luaL_setfuncs(state(), funcreg, upvalues);
|
||||
|
||||
lua_pop(state(), 1);
|
||||
return *this;
|
||||
|
@ -199,12 +193,13 @@ private:
|
|||
{ nullptr, nullptr }
|
||||
};
|
||||
|
||||
int exists = luaL_newmetatable(state(), metatablename);
|
||||
if (luaL_newmetatable(state(), metatablename) == 1) {
|
||||
lua_pushstring(state(), "__gc");
|
||||
lua_pushcclosure(state(), &lua_func::gc, 0);
|
||||
lua_settable(state(), -3);
|
||||
push();
|
||||
}
|
||||
|
||||
push();
|
||||
stack::push_user(state(), userdata, metatablename);
|
||||
luaL_setfuncs(state(), funcreg, 1);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user