Merge branch 'master' of https://github.com/ThePhD/sol into ThePhD

This commit is contained in:
Rapptz 2013-12-09 17:20:21 -05:00
commit 9e47ec6b06
3 changed files with 113 additions and 71 deletions

View File

@ -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;
@ -89,20 +121,19 @@ 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++));
}
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()));
fx_t& mem_ptr = *fxptr;
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);
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;
int upvalue = 1;
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++ ) );
}
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;
}
@ -127,7 +158,7 @@ struct lua_func {
void** puserdata = static_cast<void**>(lua_touserdata(L, 1));
void* userdata = *puserdata;
lua_func* ptr = static_cast<lua_func*>(userdata);
std::default_delete<lua_func> dx{ };
std::default_delete<lua_func> dx{};
dx(ptr);
return 0;
}

View File

@ -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) {

View File

@ -41,8 +41,8 @@ namespace detail {
}
class table : virtual public reference {
public:
table() noexcept: reference() {}
table(lua_State* L, int index = -1): reference(L, index) {
table() noexcept : reference() {}
table(lua_State* L, int index = -1) : reference(L, index) {
type_assert(L, index, type::table);
}
@ -70,7 +70,7 @@ public:
template<typename T, typename TFx>
table& set_function(T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
return set_isfunction_fx(std::is_function<clean_fx>(), std::forward<T>(key), std::forward<TFx>(fx));
return set_isfunction_fx(std::is_function<clean_fx>(), std::forward<T>(key), std::forward<TFx>(fx));
}
template<typename T, typename TFx, typename TObj>
@ -92,10 +92,10 @@ private:
template<typename T, typename TFx>
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type clean_lambda;
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
typedef typename std::decay<TFx>::type clean_lambda;
typedef typename detail::function_traits<decltype(&clean_lambda::operator())>::free_function_pointer_type raw_func_t;
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
}
template<typename T, typename TFx>
@ -107,9 +107,9 @@ private:
template<typename T, typename TFx>
table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<lua_func> sptr(new lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<lua_func> sptr(new lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx, typename TObj>
@ -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;
@ -164,49 +159,49 @@ private:
template<typename T, typename TFx>
table& set_fx(std::false_type, T&& key, TFx&& fx) {
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 ] = {
{ freefuncname, freefunc },
{ nullptr, nullptr }
};
std::string fkey(key);
ptr_fx target(std::forward<TFx>(fx));
lua_CFunction freefunc = &static_lua_func<TFx>::call;
const char* freefuncname = fkey.c_str();
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ nullptr, nullptr }
};
push();
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;
lua_pop(state(), 1);
return *this;
}
template<typename T>
table& set_fx(T&& key, std::unique_ptr<lua_func> luafunc) {
std::string fkey(key);
std::string metakey("sol.stateful.");
metakey += fkey;
metakey += ".meta";
lua_func* target = luafunc.release();
std::string metakey("sol.stateful.");
metakey += fkey;
metakey += ".meta";
lua_func* target = luafunc.release();
void* userdata = reinterpret_cast<void*>(target);
lua_CFunction freefunc = &lua_func::call;
const char* freefuncname = fkey.c_str();
const char* metatablename = metakey.c_str();
const luaL_Reg funcreg[2] = {
const char* metatablename = metakey.c_str();
const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc },
{ nullptr, nullptr }
};
{ nullptr, nullptr }
};
int exists = luaL_newmetatable(state(), metatablename);
lua_pushstring(state(), "__gc");
lua_pushcclosure(state(), &lua_func::gc, 0);
lua_settable(state(), -3);
push();
if (luaL_newmetatable(state(), metatablename) == 1) {
lua_pushstring(state(), "__gc");
lua_pushcclosure(state(), &lua_func::gc, 0);
lua_settable(state(), -3);
}
stack::push_user(state(), userdata, metatablename);
luaL_setfuncs(state(), funcreg, 1);
push();
stack::push_user(state(), userdata, metatablename);
luaL_setfuncs(state(), funcreg, 1);
lua_pop(state(), 1);
return *this;