mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Removed std::unordered_map storage on tables since they were getting deleted anyways.
Memory leaks are currently present: will have to figure out how to patch those up.
This commit is contained in:
parent
51f7010363
commit
21142e7e7d
|
@ -42,11 +42,9 @@ namespace detail {
|
|||
}
|
||||
}
|
||||
class table : virtual public reference {
|
||||
private:
|
||||
std::unordered_map<std::string, std::shared_ptr<lua_func>> funcs;
|
||||
public:
|
||||
table() noexcept: reference(), funcs() {}
|
||||
table(lua_State* L, int index = -1): reference(L, index), funcs() {
|
||||
table() noexcept: reference() {}
|
||||
table(lua_State* L, int index = -1): reference(L, index) {
|
||||
type_assert(L, index, type::table);
|
||||
}
|
||||
|
||||
|
@ -98,7 +96,7 @@ private:
|
|||
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;
|
||||
typedef std::is_convertible<clean_lambda, raw_func_t> isconvertible;
|
||||
return set_isconvertible_fx(isconvertible(), std::forward<T>(key), std::forward<TFx>(fx));
|
||||
}
|
||||
|
||||
|
@ -190,31 +188,33 @@ private:
|
|||
template<typename T>
|
||||
table& set_fx(T&& key, std::unique_ptr<lua_func> luafunc) {
|
||||
std::string fkey(key);
|
||||
auto hint = funcs.find(fkey);
|
||||
if (hint == funcs.end()) {
|
||||
std::shared_ptr<lua_func> sptr(luafunc.release());
|
||||
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
|
||||
}
|
||||
else {
|
||||
hint->second.reset(luafunc.release());
|
||||
}
|
||||
lua_func* target = hint->second.get();
|
||||
|
||||
lua_func* target = luafunc.release();
|
||||
void* userdata = reinterpret_cast<void*>(target);
|
||||
lua_CFunction freefunc = &lua_func::call;
|
||||
const char* freefuncname = hint->first.c_str();
|
||||
const luaL_Reg funcreg[ 2 ] = {
|
||||
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);
|
||||
|
||||
// Actual function call we want
|
||||
stack::push( state(), userdata );
|
||||
luaL_setfuncs(state(), funcreg, 1);
|
||||
|
||||
lua_pop(state(), 1);
|
||||
return *this;
|
||||
}
|
||||
|
||||
static int destroyfunc( lua_State* L ) {
|
||||
void* userdata = lua_touserdata( L, 1 );
|
||||
lua_func* ptr = static_cast<lua_func*>( userdata );
|
||||
std::default_delete<lua_func> dx{ };
|
||||
dx( ptr );
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
} // sol
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user