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 {
|
class table : virtual public reference {
|
||||||
private:
|
|
||||||
std::unordered_map<std::string, std::shared_ptr<lua_func>> funcs;
|
|
||||||
public:
|
public:
|
||||||
table() noexcept: reference(), funcs() {}
|
table() noexcept: reference() {}
|
||||||
table(lua_State* L, int index = -1): reference(L, index), funcs() {
|
table(lua_State* L, int index = -1): reference(L, index) {
|
||||||
type_assert(L, index, type::table);
|
type_assert(L, index, type::table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +96,7 @@ private:
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,31 +188,33 @@ private:
|
||||||
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);
|
||||||
auto hint = funcs.find(fkey);
|
|
||||||
if (hint == funcs.end()) {
|
lua_func* target = luafunc.release();
|
||||||
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();
|
|
||||||
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 = hint->first.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();
|
// Actual function call we want
|
||||||
|
stack::push( state(), userdata );
|
||||||
stack::push(state(), userdata);
|
luaL_setfuncs(state(), funcreg, 1);
|
||||||
luaL_setfuncs(state(), funcreg, 1);
|
|
||||||
|
|
||||||
lua_pop(state(), 1);
|
lua_pop(state(), 1);
|
||||||
return *this;
|
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
|
} // sol
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user