Skip to content

Commit

Permalink
fixes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Oct 9, 2023
1 parent 14a8e26 commit a5098d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
19 changes: 19 additions & 0 deletions binding/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@ namespace bee::lua {
return *o;
}

template <typename T, typename... Args>
T& newudata(lua_State* L, const char* name, void (*init_metatable)(lua_State*), Args&&... args) {
int nupvalue = 0;
if constexpr (udata_has_nupvalue<T>::value) {
nupvalue = udata<T>::nupvalue;
}
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);
}
lua_setmetatable(L, -2);
return *o;
}

template <typename T>
T& checkudata(lua_State* L, int arg) {
if constexpr (udata_has_name<T>::value) {
Expand Down
11 changes: 2 additions & 9 deletions binding/lua_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
#include <bee/nonstd/unreachable.h>
#include <bee/thread/simplethread.h>

namespace bee::lua {
template <>
struct udata<net::fd_t> {
static inline auto name = "bee::net::fd";
};
}

namespace bee::lua_socket {
static int push_neterror(lua_State* L, std::string_view msg) {
auto error = make_neterror(msg);
Expand Down Expand Up @@ -369,10 +362,10 @@ namespace bee::lua_socket {
luaL_setfuncs(L, mt, 0);
}
static void pushfd(lua_State* L, net::fd_t fd) {
lua::newudata<net::fd_t>(L, metatable, fd);
lua::newudata<net::fd_t>(L, "bee::net::fd", metatable, fd);
}
static void pushfd_no_ownership(lua_State* L, net::fd_t fd) {
lua::newudata<net::fd_t>(L, metatable_no_ownership, fd);
lua::newudata<net::fd_t>(L, "bee::net::fd (no ownership)", metatable_no_ownership, fd);
}
static int pair(lua_State* L) {
net::fd_t sv[2];
Expand Down

0 comments on commit a5098d5

Please sign in to comment.