diff --git a/sol/lua_function.hpp b/sol/lua_function.hpp index dc904227..99d54439 100644 --- a/sol/lua_function.hpp +++ b/sol/lua_function.hpp @@ -85,7 +85,7 @@ struct explicit_lua_func : public lua_func { template int operator()(types, types t, lua_State* L) { auto r = stack::pop_call(L, fx, t); - stack::push(L, r); + stack::push(L, std::move( r )); return sizeof...(TRn); } }; diff --git a/sol/stack.hpp b/sol/stack.hpp index e98c1535..08a79895 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -139,8 +139,16 @@ inline void push(lua_State* L, const nil_t&) { lua_pushnil(L); } -inline void push(lua_State* L, lua_CFunction func) { - lua_pushcfunction(L, func); +inline void push( lua_State* L, lua_CFunction func ) { + lua_pushcfunction( L, func ); +} + +inline void push( lua_State* L, lua_CFunction func, int n ) { + lua_pushcclosure( L, func, n ); +} + +inline void push( lua_State* L, void* userdata ) { + lua_pushlightuserdata( L, userdata ); } template diff --git a/sol/state.hpp b/sol/state.hpp index cccf03de..de6e3cb5 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -35,7 +35,8 @@ template struct are_same : std::integral_constant::value && are_same::value> {}; int atpanic(lua_State* L) { - throw sol_error(lua_tostring(L, -1)); + std::string err = lua_tostring( L, -1 ); + throw sol_error( err ); } } // detail @@ -76,7 +77,7 @@ public: template void open_libraries(Args&&... args) { - static_assert(detail::are_same{}, "all types must be libraries"); + static_assert(detail::are_same::value, "all types must be libraries"); if(sizeof...(args) == 0) { luaL_openlibs(L.get()); return; diff --git a/sol/table.hpp b/sol/table.hpp index 8a83a40f..8d434fcb 100644 --- a/sol/table.hpp +++ b/sol/table.hpp @@ -32,8 +32,8 @@ class table : virtual public reference { private: std::unordered_map> funcs; public: - table() noexcept: reference{} {} - table(lua_State* L, int index = -1): reference(L, index) { + table() noexcept: reference(), funcs() {} + table(lua_State* L, int index = -1): reference(L, index), funcs() { type_assert(L, index, type::table); } @@ -92,22 +92,38 @@ private: } template - table& set_fx(T&& key, std::unique_ptr funcptr) { + table& set_fx(T&& key, std::unique_ptr luafunc) { std::string fkey(key); auto hint = funcs.find(fkey); if (hint == funcs.end()) { - std::shared_ptr sptr(funcptr.release()); + std::shared_ptr sptr(luafunc.release()); hint = funcs.emplace_hint(hint, fkey, std::move(sptr)); } else { - hint->second.reset(funcptr.release()); + hint->second.reset(luafunc.release()); } detail::lua_func* target = hint->second.get(); + void* userdata = static_cast( target ); lua_CFunction freefunc = &detail::lua_cfun; - lua_pushlightuserdata(state(), static_cast(target)); - lua_pushcclosure(state(), freefunc, 1); - lua_setglobal(state(), fkey.c_str()); - return *this; + const char* freefuncname = hint->first.c_str( ); + const luaL_Reg funcreg[ 2 ] = { + { freefuncname, freefunc }, + { } + }; + + push( ); + + /*//lua_pushlightuserdata( state(), userdata ); + lua_pushstring( state(), freefuncname ); // push key onto stack + lua_pushcclosure( state(), freefunc, 1 ); // push value onto stack + lua_settable( state(), -3 ); + */ + + lua_pushlightuserdata( state( ), userdata ); + luaL_setfuncs( state( ), funcreg, 1 ); + + lua_pop( state( ), 1 ); + return *this; } }; } // sol