Okay, so now it compiles without errors or warnings and works pretty well. This should be the last piece necessary for full working conformance. Yey~

This commit is contained in:
ThePhD 2013-12-09 17:01:52 -05:00
parent 19d01ecd1d
commit e53c47f7b1
3 changed files with 113 additions and 71 deletions

View File

@ -27,6 +27,37 @@
#include <memory> #include <memory>
namespace sol { 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> template<typename TFx>
struct static_lua_func { struct static_lua_func {
@ -47,10 +78,9 @@ struct static_lua_func {
} }
static int call(lua_State* L) { static int call(lua_State* L) {
void* functiondata = lua_touserdata(L, lua_upvalueindex(1)); int upvalue = 1;
//if (functiondata == nullptr) fx_t* fx;
// throw sol_error("call from lua to c++ function has null function pointer data"); detail::get_upvalue( L, fx, upvalue );
fx_t* fx = reinterpret_cast<fx_t*>(functiondata);
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L); int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), fx, L);
return r; return r;
} }
@ -74,7 +104,9 @@ struct static_object_lua_func {
template<typename TR, typename... Args> template<typename TR, typename... Args>
static int typed_call(types<TR>, types<Args...>, T& item, fx_t& ifx, lua_State* L) { 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...>()); auto r = stack::pop_call(L, fx, types<Args...>());
stack::push(L, std::move(r)); stack::push(L, std::move(r));
return 1; return 1;
@ -89,20 +121,19 @@ struct static_object_lua_func {
} }
static int call(lua_State* L) { static int call(lua_State* L) {
const static std::size_t data_t_count = (sizeof(fx_t)+(sizeof(void*)-1)) / sizeof(void*); 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; typedef std::array<void*, data_t_count> data_t;
data_t fxptrdata; int upvalue = 1;
int upvalue = 1; data_t data = { { } };
for (std::size_t i = 0; i < fxptrdata.size(); ++i) { fx_t* fxptr;
fxptrdata[ i ] = lua_touserdata(L, lua_upvalueindex(upvalue++)); 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) fxptr = reinterpret_cast<fx_t*>( static_cast<void*>( data.data( ) ) );
// throw sol_error("call from lua to c++ function has null object data"); fx_t& mem_ptr = *fxptr;
fx_t* fxptr = reinterpret_cast<fx_t*>(static_cast<void*>(fxptrdata.data())); void* objectdata = lua_touserdata( L, lua_upvalueindex( upvalue++ ) );
fx_t& mem_ptr = *fxptr; T& obj = *static_cast<T*>( objectdata );
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 );
int r = typed_call(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), obj, mem_ptr, L);
return r; return r;
} }
@ -127,7 +158,7 @@ struct lua_func {
void** puserdata = static_cast<void**>(lua_touserdata(L, 1)); void** puserdata = static_cast<void**>(lua_touserdata(L, 1));
void* userdata = *puserdata; void* userdata = *puserdata;
lua_func* ptr = static_cast<lua_func*>(userdata); lua_func* ptr = static_cast<lua_func*>(userdata);
std::default_delete<lua_func> dx{ }; std::default_delete<lua_func> dx{};
dx(ptr); dx(ptr);
return 0; return 0;
} }

View File

@ -27,6 +27,7 @@
#include <utility> #include <utility>
#include <type_traits> #include <type_traits>
#include <array> #include <array>
#include <cstring>
namespace sol { namespace sol {
template<typename T, typename R = void> 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 { namespace detail {
template<typename T, std::size_t... I> template<typename T, std::size_t... I>
inline void push(lua_State* L, indices<I...>, const T& tuplen) { inline void push(lua_State* L, indices<I...>, const T& tuplen) {

View File

@ -41,8 +41,8 @@ namespace detail {
} }
class table : virtual public reference { class table : virtual public reference {
public: public:
table() noexcept: reference() {} table() noexcept : reference() {}
table(lua_State* L, int index = -1): reference(L, index) { table(lua_State* L, int index = -1) : reference(L, index) {
type_assert(L, index, type::table); type_assert(L, index, type::table);
} }
@ -70,12 +70,12 @@ public:
template<typename T, typename TFx> template<typename T, typename TFx>
table& set_function(T&& key, TFx&& fx) { table& set_function(T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_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> template<typename T, typename TFx, typename TObj>
table& set_function(T&& key, TFx&& fx, TObj&& obj) { table& set_function(T&& key, TFx&& fx, TObj&& obj) {
return set_lvalue_fx(std::integral_constant<bool, std::is_lvalue_reference<TObj>::value || std::is_pointer<TObj>::value>(), return set_lvalue_fx(std::integral_constant<bool, std::is_lvalue_reference<TObj>::value || std::is_pointer<TObj>::value>(),
std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj)); std::forward<T>(key), std::forward<TFx>(fx), std::forward<TObj>(obj));
} }
@ -92,10 +92,10 @@ private:
template<typename T, typename TFx> template<typename T, typename TFx>
table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) { table& set_isfunction_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type clean_lambda; 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 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; typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx)); return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
} }
template<typename T, typename TFx> template<typename T, typename TFx>
@ -107,9 +107,9 @@ private:
template<typename T, typename TFx> template<typename T, typename TFx>
table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) { table& set_isconvertible_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_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))); 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)); return set_fx(std::forward<T>(key), std::move(sptr));
} }
template<typename T, typename TFx, typename TObj> template<typename T, typename TFx, typename TObj>
@ -128,20 +128,15 @@ private:
table& set_fx(std::true_type, T&& key, TFx&& fx, TObj&& obj) { 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<TObj>::type decay_of_to;
typedef typename std::decay<TFx>::type decay_of_tfx; 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);
std::string fkey(key);
// Layout: // Layout:
// idx 1...n: verbatim data of member function pointer // idx 1...n: verbatim data of member function pointer
// idx n + 1: is the object's void pointer // idx n + 1: is the object's void pointer
// We don't need to store the size, because the other side is templated // We don't need to store the size, because the other side is templated
// with the same member function pointer type // with the same member function pointer type
decay_of_tfx fxptr(std::forward<TFx>(fx)); 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)); void* userobjdata = static_cast<void*>(detail::get_ptr(obj));
lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call; lua_CFunction freefunc = &static_object_lua_func<decay_of_to, TFx>::call;
const char* freefuncname = fkey.c_str(); const char* freefuncname = fkey.c_str();
@ -150,12 +145,12 @@ private:
{ nullptr, nullptr } { nullptr, nullptr }
}; };
push(); push();
stack::push(state(), fxptrdata); int upvalues = stack::push_user(state(), fxptr);
stack::push(state(), userobjdata); stack::push(state(), userobjdata);
luaL_setfuncs(state(), funcreg, fxptrdata.size() + 1); luaL_setfuncs(state(), funcreg, upvalues + 1);
lua_pop(state(), 1); lua_pop(state(), 1);
return *this; return *this;
@ -164,50 +159,50 @@ private:
template<typename T, typename TFx> template<typename T, typename TFx>
table& set_fx(std::false_type, T&& key, TFx&& fx) { table& set_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type ptr_fx; typedef typename std::decay<TFx>::type ptr_fx;
std::string fkey(key); std::string fkey(key);
ptr_fx target(std::forward<TFx>(fx)); ptr_fx target(std::forward<TFx>(fx));
void* userdata = reinterpret_cast<void*>(target); lua_CFunction freefunc = &static_lua_func<TFx>::call;
lua_CFunction freefunc = &static_lua_func<TFx>::call; const char* freefuncname = fkey.c_str();
const char* freefuncname = fkey.c_str(); const luaL_Reg funcreg[ 2 ] = {
const luaL_Reg funcreg[ 2 ] = { { freefuncname, freefunc },
{ freefuncname, freefunc }, { nullptr, nullptr }
{ nullptr, nullptr } };
};
push(); push();
stack::push(state(), userdata); int upvalues = stack::push_user( state( ), target );
luaL_setfuncs(state(), funcreg, 1); luaL_setfuncs(state(), funcreg, upvalues);
lua_pop(state(), 1); lua_pop(state(), 1);
return *this; return *this;
} }
template<typename T> template<typename T>
table& set_fx(T&& key, std::unique_ptr<lua_func> luafunc) { table& set_fx(T&& key, std::unique_ptr<lua_func> luafunc) {
std::string fkey(key); std::string fkey(key);
std::string metakey("sol.stateful."); std::string metakey("sol.stateful.");
metakey += fkey; metakey += fkey;
metakey += ".meta"; metakey += ".meta";
lua_func* target = luafunc.release(); lua_func* target = luafunc.release();
void* userdata = reinterpret_cast<void*>(target); void* userdata = reinterpret_cast<void*>(target);
lua_CFunction freefunc = &lua_func::call; lua_CFunction freefunc = &lua_func::call;
const char* freefuncname = fkey.c_str(); const char* freefuncname = fkey.c_str();
const char* metatablename = metakey.c_str(); const char* metatablename = metakey.c_str();
const luaL_Reg funcreg[2] = { const luaL_Reg funcreg[ 2 ] = {
{ freefuncname, freefunc }, { freefuncname, freefunc },
{ nullptr, nullptr } { nullptr, nullptr }
}; };
int exists = luaL_newmetatable(state(), metatablename); if (luaL_newmetatable(state(), metatablename) == 1) {
lua_pushstring(state(), "__gc"); lua_pushstring(state(), "__gc");
lua_pushcclosure(state(), &lua_func::gc, 0); lua_pushcclosure(state(), &lua_func::gc, 0);
lua_settable(state(), -3); lua_settable(state(), -3);
push(); }
push();
stack::push_user(state(), userdata, metatablename);
luaL_setfuncs(state(), funcreg, 1);
stack::push_user(state(), userdata, metatablename);
luaL_setfuncs(state(), funcreg, 1);
lua_pop(state(), 1); lua_pop(state(), 1);
return *this; return *this;
} }