git: f56402485dd5 - stable/14 - lposix: Clean up the posix namespace definitions
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 11 Sep 2025 18:50:22 UTC
The branch stable/14 has been updated by emaste:
URL: https://cgit.FreeBSD.org/src/commit/?id=f56402485dd54c7a2d88cb145864bba59b8d4532
commit f56402485dd54c7a2d88cb145864bba59b8d4532
Author: Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2025-07-07 15:43:27 +0000
Commit: Ed Maste <emaste@FreeBSD.org>
CommitDate: 2025-09-11 15:09:58 +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
(cherry picked from commit eda96744b434325c475dce449744f2268f1033e8)
---
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 43b9ee83673c..4ccfcdaa46e7 100644
--- a/libexec/flua/modules/lposix.c
+++ b/libexec/flua/modules/lposix.c
@@ -533,21 +533,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);
@@ -565,14 +565,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);
@@ -598,16 +605,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);