Expose lush global as a standard library (issue #26)

Register luaopen_lush() in stdlibs so the lush table is available as a
global. Users can now extend the shell by adding functions to
lush.builtins. The OP_LUSH VM access via integer keys is preserved.
This commit is contained in:
Cormac Shannon
2026-03-18 09:52:56 +00:00
parent 1766e40a68
commit 768de9fe8b
6 changed files with 161 additions and 24 deletions

26
linit.c
View File

@@ -39,30 +39,11 @@ static const luaL_Reg stdlibs[] = {
{LUA_STRLIBNAME, luaopen_string},
{LUA_TABLIBNAME, luaopen_table},
{LUA_UTF8LIBNAME, luaopen_utf8},
{LUA_LUSHLIBNAME, luaopen_lush},
{NULL, NULL}
};
/*
** Store shell functions in a registry table at LUA_RIDX_LUSH.
** Integer keys 1..5 hold the C functions (accessed by OP_LUSH).
** String key "builtins" holds the builtins table (set by luaopen_builtins).
*/
static void opencommand (lua_State *L) {
lua_createtable(L, LUSH_OP_COUNT, 1);
lua_pushcfunction(L, lushCmd_command);
lua_rawseti(L, -2, LUSH_OP_COMMAND + 1);
lua_pushcfunction(L, lushCmd_interactive);
lua_rawseti(L, -2, LUSH_OP_INTERACTIVE + 1);
lua_pushcfunction(L, lushCmd_getenv);
lua_rawseti(L, -2, LUSH_OP_GETENV + 1);
lua_pushcfunction(L, lushCmd_setenv);
lua_rawseti(L, -2, LUSH_OP_SETENV + 1);
lua_pushcfunction(L, lushCmd_subcmd);
lua_rawseti(L, -2, LUSH_OP_SUBCMD + 1);
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
}
/*
** require and preload selected standard libraries
@@ -81,9 +62,8 @@ LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
}
}
lua_assert((mask >> 1) == LUA_UTF8LIBK);
lua_assert((mask >> 1) == LUA_LUSHLIBK);
lua_pop(L, 1); /* remove PRELOAD table */
opencommand(L); /* register __command global */
luaopen_builtins(L); /* register __builtins table */
luaopen_builtins(L); /* register builtins in lush table */
}