Date: Mon, 7 Jul 2025 15:44:00 GMT From: Mark Johnston <markj@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: git: eda96744b434 - main - lposix: Clean up the posix namespace definitions Message-ID: <202507071544.567Fi0ZJ092401@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=eda96744b434325c475dce449744f2268f1033e8 commit eda96744b434325c475dce449744f2268f1033e8 Author: Mark Johnston <markj@FreeBSD.org> AuthorDate: 2025-07-07 15:43:27 +0000 Commit: Mark Johnston <markj@FreeBSD.org> CommitDate: 2025-07-07 15:43:27 +0000 lposix: Clean up the posix namespace definitions The posix module is subdivided according to C headers; for instance, posix.unistd contains routines available from unistd.h, such as chown(2). A quirk of our implementation is that each of the modules is a direct entry in the global table. That is, there is no "posix" table. Instead, "posix.foo" and "posix.bar.baz" are both top-level tables. This is surprising and goes against Lua's shorthand of using "." to access keys in a table. lua-posix also doesn't work this way. Rework things so that "posix" and "posix.sys" are proper tables. Existing flua code which uses require() to bind posix submodules to a name will be unaffected. Code which accesses them directly using something like _G["posix.sys.utsname"].uname() will be broken, but I don't think anything like that exists. In particular, it is now possible to call posix.sys.utsname.uname() without any require statements. Reviewed by: imp, bapt MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D51158 --- libexec/flua/linit_flua.c | 10 +-------- libexec/flua/modules/lposix.c | 51 +++++++++++++++++++++++++++++++++---------- libexec/flua/modules/lposix.h | 8 +------ 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/libexec/flua/linit_flua.c b/libexec/flua/linit_flua.c index 8eaa4af1ffca..b466b7872158 100644 --- a/libexec/flua/linit_flua.c +++ b/libexec/flua/linit_flua.c @@ -57,18 +57,11 @@ static const luaL_Reg loadedlibs[] = { #endif /* FreeBSD Extensions */ {"lfs", luaopen_lfs}, - {"posix.fnmatch", luaopen_posix_fnmatch}, - {"posix.libgen", luaopen_posix_libgen}, - {"posix.stdlib", luaopen_posix_stdlib}, - {"posix.sys.stat", luaopen_posix_sys_stat}, - {"posix.sys.utsname", luaopen_posix_sys_utsname}, - {"posix.sys.wait", luaopen_posix_sys_wait}, - {"posix.unistd", luaopen_posix_unistd}, + {"posix", luaopen_posix}, {"fbsd", luaopen_fbsd}, {NULL, NULL} }; - LUALIB_API void luaL_openlibs (lua_State *L) { const luaL_Reg *lib; /* "require" functions from 'loadedlibs' and set results to global table */ @@ -77,4 +70,3 @@ LUALIB_API void luaL_openlibs (lua_State *L) { lua_pop(L, 1); /* remove lib */ } } - diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c index 9edc7e687786..75cdd345aeaa 100644 --- a/libexec/flua/modules/lposix.c +++ b/libexec/flua/modules/lposix.c @@ -590,21 +590,21 @@ static const struct luaL_Reg unistdlib[] = { #undef REG_SIMPLE #undef REG_DEF -int +static int luaopen_posix_libgen(lua_State *L) { luaL_newlib(L, libgenlib); return (1); } -int +static int luaopen_posix_stdlib(lua_State *L) { luaL_newlib(L, stdliblib); return (1); } -int +static int luaopen_posix_fnmatch(lua_State *L) { luaL_newlib(L, fnmatchlib); @@ -622,14 +622,21 @@ luaopen_posix_fnmatch(lua_State *L) return 1; } -int +static int luaopen_posix_sys_stat(lua_State *L) { luaL_newlib(L, sys_statlib); return (1); } -int +static int +luaopen_posix_sys_utsname(lua_State *L) +{ + luaL_newlib(L, sys_utsnamelib); + return 1; +} + +static int luaopen_posix_sys_wait(lua_State *L) { luaL_newlib(L, sys_waitlib); @@ -655,16 +662,38 @@ luaopen_posix_sys_wait(lua_State *L) return (1); } -int -luaopen_posix_sys_utsname(lua_State *L) +static int +luaopen_posix_unistd(lua_State *L) { - luaL_newlib(L, sys_utsnamelib); - return 1; + luaL_newlib(L, unistdlib); + return (1); } int -luaopen_posix_unistd(lua_State *L) +luaopen_posix(lua_State *L) { - luaL_newlib(L, unistdlib); + lua_newtable(L); /* posix */ + + luaL_requiref(L, "posix.fnmatch", luaopen_posix_fnmatch, 0); + lua_setfield(L, -2, "fnmatch"); + + luaL_requiref(L, "posix.libgen", luaopen_posix_libgen, 0); + lua_setfield(L, -2, "libgen"); + + luaL_requiref(L, "posix.stdlib", luaopen_posix_stdlib, 0); + lua_setfield(L, -2, "stdlib"); + + lua_newtable(L); /* posix.sys */ + luaL_requiref(L, "posix.sys.stat", luaopen_posix_sys_stat, 0); + lua_setfield(L, -2, "stat"); + luaL_requiref(L, "posix.sys.utsname", luaopen_posix_sys_utsname, 0); + lua_setfield(L, -2, "utsname"); + luaL_requiref(L, "posix.sys.wait", luaopen_posix_sys_wait, 0); + lua_setfield(L, -2, "wait"); + lua_setfield(L, -2, "sys"); + + luaL_requiref(L, "posix.unistd", luaopen_posix_unistd, 0); + lua_setfield(L, -2, "unistd"); + return (1); } diff --git a/libexec/flua/modules/lposix.h b/libexec/flua/modules/lposix.h index da7079056826..1aa33f042571 100644 --- a/libexec/flua/modules/lposix.h +++ b/libexec/flua/modules/lposix.h @@ -7,10 +7,4 @@ #include <lua.h> -int luaopen_posix_fnmatch(lua_State *L); -int luaopen_posix_libgen(lua_State *L); -int luaopen_posix_stdlib(lua_State *L); -int luaopen_posix_sys_stat(lua_State *L); -int luaopen_posix_sys_utsname(lua_State *L); -int luaopen_posix_sys_wait(lua_State *L); -int luaopen_posix_unistd(lua_State *L); +int luaopen_posix(lua_State *L);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202507071544.567Fi0ZJ092401>