Skip to content

Commit

Permalink
add getmetatable
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Dec 7, 2023
1 parent 2d2acf6 commit ac896eb
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions binding/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,28 @@ namespace bee::lua {
}
}

template <typename T>
void getmetatable(lua_State* L, void (*init_metatable)(lua_State*)) {
if (luaL_newmetatable(L, udata<T>::name)) {
if constexpr (!std::is_trivially_destructible<T>::value) {
lua_pushcfunction(L, destroyudata<T>);
lua_setfield(L, -2, "__gc");
}
init_metatable(L);
}
}

template <typename T>
void getmetatable(lua_State* L, const char* name, void (*init_metatable)(lua_State*)) {
if (luaL_newmetatable(L, name)) {
if constexpr (!std::is_trivially_destructible<T>::value) {
lua_pushcfunction(L, destroyudata<T>);
lua_setfield(L, -2, "__gc");
}
init_metatable(L);
}
}

template <typename T, typename... Args>
T& newudata(lua_State* L, void (*init_metatable)(lua_State*), Args&&... args) {
static_assert(udata_has_name<T>::value);
Expand All @@ -221,13 +243,7 @@ namespace bee::lua {
}
T* o = static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nupvalue));
new (o) T(std::forward<Args>(args)...);
if (luaL_newmetatable(L, udata<T>::name)) {
if constexpr (!std::is_trivially_destructible<T>::value) {
lua_pushcfunction(L, destroyudata<T>);
lua_setfield(L, -2, "__gc");
}
init_metatable(L);
}
getmetatable<T>(L, init_metatable);
lua_setmetatable(L, -2);
return *o;
}
Expand All @@ -240,13 +256,7 @@ namespace bee::lua {
}
T* o = static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nupvalue));
new (o) T(std::forward<Args>(args)...);
if (luaL_newmetatable(L, name)) {
if constexpr (!std::is_trivially_destructible<T>::value) {
lua_pushcfunction(L, destroyudata<T>);
lua_setfield(L, -2, "__gc");
}
init_metatable(L);
}
getmetatable<T>(L, name, init_metatable);
lua_setmetatable(L, -2);
return *o;
}
Expand Down

0 comments on commit ac896eb

Please sign in to comment.