diff --git a/binding/binding.h b/binding/binding.h index 19a5467a..c1179c82 100644 --- a/binding/binding.h +++ b/binding/binding.h @@ -175,17 +175,6 @@ namespace bee::lua { return 0; } - template - T& newudata(lua_State* L) { - static_assert(!udata_has_name::value); - static_assert(std::is_trivial::value); - int nupvalue = 0; - if constexpr (udata_has_nupvalue::value) { - nupvalue = udata::nupvalue; - } - return *static_cast(lua_newuserdatauv(L, sizeof(T), nupvalue)); - } - namespace cxx { struct status { int n; @@ -213,29 +202,18 @@ namespace bee::lua { } template - void getmetatable(lua_State* L, void (*init_metatable)(lua_State*)) { + void getmetatable(lua_State* L) { if (luaL_newmetatable(L, udata::name)) { if constexpr (!std::is_trivially_destructible::value) { lua_pushcfunction(L, destroyudata); lua_setfield(L, -2, "__gc"); } - init_metatable(L); - } - } - - template - void getmetatable(lua_State* L, const char* name, void (*init_metatable)(lua_State*)) { - if (luaL_newmetatable(L, name)) { - if constexpr (!std::is_trivially_destructible::value) { - lua_pushcfunction(L, destroyudata); - lua_setfield(L, -2, "__gc"); - } - init_metatable(L); + udata::metatable(L); } } template - T& newudata(lua_State* L, void (*init_metatable)(lua_State*), Args&&... args) { + T& newudata(lua_State* L, Args&&... args) { static_assert(udata_has_name::value); int nupvalue = 0; if constexpr (udata_has_nupvalue::value) { @@ -243,20 +221,7 @@ namespace bee::lua { } T* o = static_cast(lua_newuserdatauv(L, sizeof(T), nupvalue)); new (o) T(std::forward(args)...); - getmetatable(L, init_metatable); - lua_setmetatable(L, -2); - return *o; - } - - template - T& newudata(lua_State* L, const char* name, void (*init_metatable)(lua_State*), Args&&... args) { - int nupvalue = 0; - if constexpr (udata_has_nupvalue::value) { - nupvalue = udata::nupvalue; - } - T* o = static_cast(lua_newuserdatauv(L, sizeof(T), nupvalue)); - new (o) T(std::forward(args)...); - getmetatable(L, name, init_metatable); + getmetatable(L); lua_setmetatable(L, -2); return *o; } diff --git a/binding/lua_filesystem.cpp b/binding/lua_filesystem.cpp index 950c1349..89e1cf6f 100644 --- a/binding/lua_filesystem.cpp +++ b/binding/lua_filesystem.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -15,26 +14,6 @@ # define BEE_DISABLE_FULLPATH #endif -namespace bee::lua { - template <> - struct udata { - static inline auto name = "bee::file_status"; - }; - template <> - struct udata { - static inline auto name = "bee::directory_entry"; - }; - template <> - struct udata { - static inline auto name = "bee::pairs_r"; - }; - template <> - struct udata { - static inline auto name = "bee::pairs"; - }; - -} - #if defined(__EMSCRIPTEN__) # include namespace bee::lua_filesystem { @@ -395,13 +374,13 @@ namespace bee::lua_filesystem { luaL_setfuncs(L, mt, 0); } static void push(lua_State* L) { - lua::newudata(L, metatable); + lua::newudata(L); } static void push(lua_State* L, const fs::path& path) { - lua::newudata(L, metatable, path); + lua::newudata(L, path); } static void push(lua_State* L, fs::path&& path) { - lua::newudata(L, metatable, std::forward(path)); + lua::newudata(L, std::forward(path)); } } @@ -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(L, metatable, std::forward(status)); + lua::newudata(L, std::forward(status)); } } @@ -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(L, metatable, entry); + lua::newudata(L, entry); } } @@ -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(L, metatable, path, ec); + lua::newudata(L, path, ec); if (ec) { return pusherror(L, "directory_iterator::directory_iterator", ec, path); } @@ -1174,3 +1153,31 @@ namespace bee::lua_filesystem { } DEFINE_LUAOPEN(filesystem) + +namespace bee::lua { + template <> + struct udata { + static inline auto name = "bee::path"; + static inline auto metatable = bee::lua_filesystem::path::metatable; + }; + template <> + struct udata { + static inline auto name = "bee::file_status"; + static inline auto metatable = bee::lua_filesystem::file_status::metatable; + }; + template <> + struct udata { + static inline auto name = "bee::directory_entry"; + static inline auto metatable = bee::lua_filesystem::directory_entry::metatable; + }; + template <> + struct udata { + static inline auto name = "bee::pairs_r"; + static inline auto metatable = bee::lua_filesystem::pairs_directory::metatable; + }; + template <> + struct udata { + static inline auto name = "bee::pairs"; + static inline auto metatable = bee::lua_filesystem::pairs_directory::metatable; + }; +} diff --git a/binding/lua_filewatch.cpp b/binding/lua_filewatch.cpp index 5a3cda79..c57ac23a 100644 --- a/binding/lua_filewatch.cpp +++ b/binding/lua_filewatch.cpp @@ -4,14 +4,6 @@ #include #include -namespace bee::lua { - template <> - struct udata { - 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(L, idx); @@ -129,7 +121,7 @@ namespace bee::lua_filewatch { } static int create(lua_State* L) { - lua::newudata(L, metatable); + lua::newudata(L); lua_newthread(L); lua_setiuservalue(L, -2, 1); return 1; @@ -150,3 +142,12 @@ namespace bee::lua_filewatch { } DEFINE_LUAOPEN(filewatch) + +namespace bee::lua { + template <> + struct udata { + static inline int nupvalue = 1; + static inline auto name = "bee::filewatch"; + static inline auto metatable = bee::lua_filewatch::metatable; + }; +} diff --git a/binding/lua_select.cpp b/binding/lua_select.cpp index 73cb62fd..3cd22477 100644 --- a/binding/lua_select.cpp +++ b/binding/lua_select.cpp @@ -288,7 +288,7 @@ namespace bee::lua_select { luaL_setfuncs(L, mt, 0); } static int create(lua_State* L) { - lua::newudata(L, metatable); + lua::newudata(L); lua_newtable(L); lua_setiuservalue(L, -2, 1); lua_newtable(L); @@ -322,6 +322,7 @@ namespace bee::lua { template <> struct udata { 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; }; } diff --git a/binding/lua_socket.cpp b/binding/lua_socket.cpp index 48d58a75..01e60e8d 100644 --- a/binding/lua_socket.cpp +++ b/binding/lua_socket.cpp @@ -5,6 +5,12 @@ #include 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); @@ -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(L, "bee::net::fd", metatable, fd); + lua::newudata(L, fd); } static void pushfd_no_ownership(lua_State* L, net::fd_t fd) { - lua::newudata(L, "bee::net::fd (no ownership)", metatable_no_ownership, fd); + lua::newudata(L, fd); } static int pair(lua_State* L) { net::fd_t sv[2]; @@ -417,3 +423,18 @@ namespace bee::lua_socket { } DEFINE_LUAOPEN(socket) + +namespace bee::lua { + template <> + struct udata { + static inline int nupvalue = 1; + static inline auto name = "bee::net::fd"; + static inline auto metatable = bee::lua_socket::metatable; + }; + template <> + struct udata { + static inline int nupvalue = 1; + static inline auto name = "bee::net::fd (no ownership)"; + static inline auto metatable = bee::lua_socket::metatable_no_ownership; + }; +} diff --git a/binding/lua_subprocess.cpp b/binding/lua_subprocess.cpp index 9ab7fe62..ce95298b 100644 --- a/binding/lua_subprocess.cpp +++ b/binding/lua_subprocess.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -19,14 +18,6 @@ # include #endif -namespace bee::lua { - template <> - struct udata { - 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) { @@ -189,7 +180,7 @@ namespace bee::lua_subprocess { } static int constructor(lua_State* L, subprocess::spawn& spawn) { - lua::newudata(L, metatable, spawn); + lua::newudata(L, spawn); return 1; } } @@ -615,3 +606,12 @@ return table.concat(t) } DEFINE_LUAOPEN(subprocess) + +namespace bee::lua { + template <> + struct udata { + static inline int nupvalue = 1; + static inline auto name = "bee::subprocess"; + static inline auto metatable = bee::lua_subprocess::process::metatable; + }; +} diff --git a/binding/lua_thread.cpp b/binding/lua_thread.cpp index b29ebea8..851fa398 100644 --- a/binding/lua_thread.cpp +++ b/binding/lua_thread.cpp @@ -80,17 +80,6 @@ namespace bee::lua_thread { }; } -namespace bee::lua { - template <> - struct udata { - static inline auto name = "bee::channel"; - }; - template <> - struct udata { - static inline auto name = "bee::rpc"; - }; -} - namespace bee::lua_thread { class channelmgr { public: @@ -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 }, @@ -199,7 +188,7 @@ namespace bee::lua_thread { if (!c) { return luaL_error(L, "Can't query channel '%s'", name.data()); } - lua::newudata(L, metatable, c); + lua::newudata(L, c); return 1; } @@ -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(L, rpc_metatable); + auto& r = lua::newudata(L); lua_pushlightuserdata(L, &r); lua_rotate(L, 1, 1); return 2; @@ -373,3 +362,16 @@ namespace bee::lua_thread { } DEFINE_LUAOPEN(thread) + +namespace bee::lua { + template <> + struct udata { + static inline auto name = "bee::channel"; + static inline auto metatable = bee::lua_thread::channel_metatable; + }; + template <> + struct udata { + static inline auto name = "bee::rpc"; + static inline auto metatable = bee::lua_thread::rpc_metatable; + }; +} diff --git a/binding/udata.h b/binding/udata.h deleted file mode 100644 index 9267fb1d..00000000 --- a/binding/udata.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include -#include - -namespace bee::lua { - template <> - struct udata { - static inline auto name = "bee::path"; - }; -}