diff --git a/main.cpp b/main.cpp new file mode 100644 index 00000000..0532dee1 --- /dev/null +++ b/main.cpp @@ -0,0 +1,118 @@ +#include +#include + +namespace sol { + + template + struct constructors; + + template + class lua_class { + public: + static const std::string classname; + static const std::string meta; + + private: + std::string luaname; + std::vector functionnames; + std::vector functions; + std::vector functiontable; + std::vector metatable; + + struct maker { + + static int construct( lua_State* L ) { + // First argument is now a table that represent the class to instantiate + luaL_checktype( L, 1, LUA_TTABLE ); + + lua_newtable( L ); // Create table to represent instance + + // Set first argument of new to metatable of instance + lua_pushvalue( L, 1 ); + lua_setmetatable( L, -2 ); + + // Do function lookups in metatable + lua_pushvalue( L, 1 ); + lua_setfield( L, 1, "__index" ); + + void* userdata = lua_newuserdata( L, sizeof( T ) ); + T* obj = static_cast( userdata ); + std::allocator alloc{ }; + alloc.construct( obj ); + } + + template + static int destruct( lua_State* L ) { + void* userdata = lua_touserdata( L, 0 ); + T* obj = static_cast( userdata ); + std::allocator alloc{ }; + alloc.destroy( obj ); + } + }; + + void build_function_tables( ) { + + } + + template + void build_function_tables( Ret( T::* func )( MArgs... ), std::string name, Args&&... args ) { + functionnames.push_back( std::move( name ) ); + functions.emplace_back( std::move( func ) ); + functiontable.push_back( { functionnames.back().c_str(), &lua_func::call } ); + build_function_tables( std::forward( args )... ); + } + + public: + template + lua_class( Args&&... args ) : lua_class( classname, std::forward( args )... ) { + + } + + template + lua_class( std::string name, Args&&... args ) : lua_class( name, constructors<>(), std::forward( args )... ) { + + } + + template + lua_class( std::string name, constructors c, Args&&... args ) : luaname( std::move( name ) ) { + functionnames.reserve( sizeof...( args ) ); + functiontable.reserve( sizeof...( args ) ); + metatable.reserve( sizeof...( args ) ); + build_function_tables( std::forward( args )... ); + functiontable. + } + }; + + template + const std::string lua_class::classname = detail::demangle( typeid( T ) ); + + template + const std::string lua_class::meta = std::string( "sol.stateful." ).append( classname ); + +} + +struct f { + int x; + f( ) : x( 1 ) { + } + f( int x ) : x( x ) { + } + int add( int y ) { + return x + y; + } +}; + +#include + +int main( ) { + sol::state s; + f x( 20 ); + + sol::lua_class lc{ }; + std::cout << lc.classname << std::endl; + std::cout << lc.meta << std::endl; + + s.set_function( "add", &f::add, f(10) ); + s.script( "t = add(20)" ); + std::cout << s.get( "t" ); +} diff --git a/sol/demangle.hpp b/sol/demangle.hpp index 9328673b..f9d0b1b2 100644 --- a/sol/demangle.hpp +++ b/sol/demangle.hpp @@ -25,19 +25,32 @@ #include #ifdef _MSC_VER -#include +/*#include #include - +*/ namespace sol { namespace detail { std::string demangle(const std::type_info& id) { - std::string realname(2048, '\0'); - DWORD result = UnDecorateSymbolName(id.raw_name(), realname.data(), realname.size(), UNDNAME_32_BIT_DECODE); + /*std::string realname(2048, '\0'); + DWORD result = UnDecorateSymbolName(id.raw_name(), &realname[0], + static_cast(realname.size()), UNDNAME_NAME_ONLY | UNDNAME_32_BIT_DECODE); if (result == 0) return ""; realname.resize(result); + */ + const static std::string removals[ 2 ] = { + "struct ", + "class " + }; + std::string realname = id.name( ); + for ( std::size_t r = 0; r < 2; ++r ) { + auto found = realname.find( removals[ r ] ); + if ( found == std::string::npos ) + continue; + realname.erase( found, removals[r].size() ); + } return realname; }