Skip to content

Commit

Permalink
简化newudata
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Dec 9, 2023
1 parent ac896eb commit f598377
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 114 deletions.
43 changes: 4 additions & 39 deletions binding/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,6 @@ namespace bee::lua {
return 0;
}

template <typename T>
T& newudata(lua_State* L) {
static_assert(!udata_has_name<T>::value);
static_assert(std::is_trivial<T>::value);
int nupvalue = 0;
if constexpr (udata_has_nupvalue<T>::value) {
nupvalue = udata<T>::nupvalue;
}
return *static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nupvalue));
}

namespace cxx {
struct status {
int n;
Expand Down Expand Up @@ -213,50 +202,26 @@ namespace bee::lua {
}

template <typename T>
void getmetatable(lua_State* L, void (*init_metatable)(lua_State*)) {
void getmetatable(lua_State* L) {
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);
udata<T>::metatable(L);
}
}

template <typename T, typename... Args>
T& newudata(lua_State* L, void (*init_metatable)(lua_State*), Args&&... args) {
T& newudata(lua_State* L, Args&&... args) {
static_assert(udata_has_name<T>::value);
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)...);
getmetatable<T>(L, init_metatable);
lua_setmetatable(L, -2);
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)...);
getmetatable<T>(L, name, init_metatable);
getmetatable<T>(L);
lua_setmetatable(L, -2);
return *o;
}
Expand Down
61 changes: 34 additions & 27 deletions binding/lua_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <bee/utility/path_helper.h>
#include <binding/binding.h>
#include <binding/file.h>
#include <binding/udata.h>

#include <chrono>
#include <utility>
Expand All @@ -15,26 +14,6 @@
# define BEE_DISABLE_FULLPATH
#endif

namespace bee::lua {
template <>
struct udata<fs::file_status> {
static inline auto name = "bee::file_status";
};
template <>
struct udata<fs::directory_entry> {
static inline auto name = "bee::directory_entry";
};
template <>
struct udata<fs::recursive_directory_iterator> {
static inline auto name = "bee::pairs_r";
};
template <>
struct udata<fs::directory_iterator> {
static inline auto name = "bee::pairs";
};

}

#if defined(__EMSCRIPTEN__)
# include <emscripten.h>
namespace bee::lua_filesystem {
Expand Down Expand Up @@ -395,13 +374,13 @@ namespace bee::lua_filesystem {
luaL_setfuncs(L, mt, 0);
}
static void push(lua_State* L) {
lua::newudata<fs::path>(L, metatable);
lua::newudata<fs::path>(L);
}
static void push(lua_State* L, const fs::path& path) {
lua::newudata<fs::path>(L, metatable, path);
lua::newudata<fs::path>(L, path);
}
static void push(lua_State* L, fs::path&& path) {
lua::newudata<fs::path>(L, metatable, std::forward<fs::path>(path));
lua::newudata<fs::path>(L, std::forward<fs::path>(path));
}
}

Expand Down Expand Up @@ -493,7 +472,7 @@ namespace bee::lua_filesystem {
luaL_setfuncs(L, mt, 0);
}
static void push(lua_State* L, fs::file_status&& status) {
lua::newudata<fs::file_status>(L, metatable, std::forward<fs::file_status>(status));
lua::newudata<fs::file_status>(L, std::forward<fs::file_status>(status));
}
}

Expand Down Expand Up @@ -614,7 +593,7 @@ namespace bee::lua_filesystem {
luaL_setfuncs(L, mt, 0);
}
static void push(lua_State* L, const fs::directory_entry& entry) {
lua::newudata<fs::directory_entry>(L, metatable, entry);
lua::newudata<fs::directory_entry>(L, entry);
}
}

Expand Down Expand Up @@ -1011,7 +990,7 @@ namespace bee::lua_filesystem {
}
static lua::cxx::status constructor(lua_State* L, const fs::path& path) {
std::error_code ec;
lua::newudata<T>(L, metatable, path, ec);
lua::newudata<T>(L, path, ec);
if (ec) {
return pusherror(L, "directory_iterator::directory_iterator", ec, path);
}
Expand Down Expand Up @@ -1174,3 +1153,31 @@ namespace bee::lua_filesystem {
}

DEFINE_LUAOPEN(filesystem)

namespace bee::lua {
template <>
struct udata<fs::path> {
static inline auto name = "bee::path";
static inline auto metatable = bee::lua_filesystem::path::metatable;
};
template <>
struct udata<fs::file_status> {
static inline auto name = "bee::file_status";
static inline auto metatable = bee::lua_filesystem::file_status::metatable;
};
template <>
struct udata<fs::directory_entry> {
static inline auto name = "bee::directory_entry";
static inline auto metatable = bee::lua_filesystem::directory_entry::metatable;
};
template <>
struct udata<fs::recursive_directory_iterator> {
static inline auto name = "bee::pairs_r";
static inline auto metatable = bee::lua_filesystem::pairs_directory<fs::recursive_directory_iterator>::metatable;
};
template <>
struct udata<fs::directory_iterator> {
static inline auto name = "bee::pairs";
static inline auto metatable = bee::lua_filesystem::pairs_directory<fs::directory_iterator>::metatable;
};
}
19 changes: 10 additions & 9 deletions binding/lua_filewatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
#include <bee/nonstd/unreachable.h>
#include <binding/binding.h>

namespace bee::lua {
template <>
struct udata<filewatch::watch> {
static inline int nupvalue = 1;
static inline auto name = "bee::filewatch";
};
}

namespace bee::lua_filewatch {
static filewatch::watch& to(lua_State* L, int idx) {
return lua::checkudata<filewatch::watch>(L, idx);
Expand Down Expand Up @@ -129,7 +121,7 @@ namespace bee::lua_filewatch {
}

static int create(lua_State* L) {
lua::newudata<filewatch::watch>(L, metatable);
lua::newudata<filewatch::watch>(L);
lua_newthread(L);
lua_setiuservalue(L, -2, 1);
return 1;
Expand All @@ -150,3 +142,12 @@ namespace bee::lua_filewatch {
}

DEFINE_LUAOPEN(filewatch)

namespace bee::lua {
template <>
struct udata<filewatch::watch> {
static inline int nupvalue = 1;
static inline auto name = "bee::filewatch";
static inline auto metatable = bee::lua_filewatch::metatable;
};
}
5 changes: 3 additions & 2 deletions binding/lua_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ namespace bee::lua_select {
luaL_setfuncs(L, mt, 0);
}
static int create(lua_State* L) {
lua::newudata<select_ctx>(L, metatable);
lua::newudata<select_ctx>(L);
lua_newtable(L);
lua_setiuservalue(L, -2, 1);
lua_newtable(L);
Expand Down Expand Up @@ -322,6 +322,7 @@ namespace bee::lua {
template <>
struct udata<lua_select::select_ctx> {
static inline int nupvalue = 4;
static inline auto name = "bee::select";
static inline auto name = "bee::select";
static inline auto metatable = bee::lua_select::metatable;
};
}
25 changes: 23 additions & 2 deletions binding/lua_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include <binding/binding.h>

namespace bee::lua_socket {
struct fd_no_ownership {
net::fd_t v;
fd_no_ownership(net::fd_t v)
: v(v)
{}
};
static int push_neterror(lua_State* L, std::string_view msg) {
auto error = make_neterror(msg);
lua_pushnil(L);
Expand Down Expand Up @@ -354,10 +360,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, "bee::net::fd", metatable, fd);
lua::newudata<net::fd_t>(L, fd);
}
static void pushfd_no_ownership(lua_State* L, net::fd_t fd) {
lua::newudata<net::fd_t>(L, "bee::net::fd (no ownership)", metatable_no_ownership, fd);
lua::newudata<fd_no_ownership>(L, fd);
}
static int pair(lua_State* L) {
net::fd_t sv[2];
Expand Down Expand Up @@ -417,3 +423,18 @@ namespace bee::lua_socket {
}

DEFINE_LUAOPEN(socket)

namespace bee::lua {
template <>
struct udata<net::fd_t> {
static inline int nupvalue = 1;
static inline auto name = "bee::net::fd";
static inline auto metatable = bee::lua_socket::metatable;
};
template <>
struct udata<lua_socket::fd_no_ownership> {
static inline int nupvalue = 1;
static inline auto name = "bee::net::fd (no ownership)";
static inline auto metatable = bee::lua_socket::metatable_no_ownership;
};
}
20 changes: 10 additions & 10 deletions binding/lua_subprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <bee/utility/assume.h>
#include <binding/binding.h>
#include <binding/file.h>
#include <binding/udata.h>
#include <errno.h>
#include <signal.h>

Expand All @@ -19,14 +18,6 @@
# include <unistd.h>
#endif

namespace bee::lua {
template <>
struct udata<subprocess::process> {
static inline int nupvalue = 1;
static inline auto name = "bee::subprocess";
};
}

namespace bee::lua_subprocess {
namespace process {
static auto& to(lua_State* L, int idx) {
Expand Down Expand Up @@ -189,7 +180,7 @@ namespace bee::lua_subprocess {
}

static int constructor(lua_State* L, subprocess::spawn& spawn) {
lua::newudata<subprocess::process>(L, metatable, spawn);
lua::newudata<subprocess::process>(L, spawn);
return 1;
}
}
Expand Down Expand Up @@ -615,3 +606,12 @@ return table.concat(t)
}

DEFINE_LUAOPEN(subprocess)

namespace bee::lua {
template <>
struct udata<subprocess::process> {
static inline int nupvalue = 1;
static inline auto name = "bee::subprocess";
static inline auto metatable = bee::lua_subprocess::process::metatable;
};
}
30 changes: 16 additions & 14 deletions binding/lua_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ namespace bee::lua_thread {
};
}

namespace bee::lua {
template <>
struct udata<lua_thread::boxchannel> {
static inline auto name = "bee::channel";
};
template <>
struct udata<lua_thread::rpc> {
static inline auto name = "bee::rpc";
};
}

namespace bee::lua_thread {
class channelmgr {
public:
Expand Down Expand Up @@ -181,7 +170,7 @@ namespace bee::lua_thread {
return 0;
}

static void metatable(lua_State* L) {
static void channel_metatable(lua_State* L) {
luaL_Reg lib[] = {
{ "push", lchannel_push },
{ "pop", lchannel_pop },
Expand All @@ -199,7 +188,7 @@ namespace bee::lua_thread {
if (!c) {
return luaL_error(L, "Can't query channel '%s'", name.data());
}
lua::newudata<boxchannel>(L, metatable, c);
lua::newudata<boxchannel>(L, c);
return 1;
}

Expand Down Expand Up @@ -318,7 +307,7 @@ namespace bee::lua_thread {
static void rpc_metatable(lua_State* L) {}

static int lrpc_create(lua_State* L) {
auto& r = lua::newudata<rpc>(L, rpc_metatable);
auto& r = lua::newudata<rpc>(L);
lua_pushlightuserdata(L, &r);
lua_rotate(L, 1, 1);
return 2;
Expand Down Expand Up @@ -373,3 +362,16 @@ namespace bee::lua_thread {
}

DEFINE_LUAOPEN(thread)

namespace bee::lua {
template <>
struct udata<lua_thread::boxchannel> {
static inline auto name = "bee::channel";
static inline auto metatable = bee::lua_thread::channel_metatable;
};
template <>
struct udata<lua_thread::rpc> {
static inline auto name = "bee::rpc";
static inline auto metatable = bee::lua_thread::rpc_metatable;
};
}
Loading

0 comments on commit f598377

Please sign in to comment.