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

31
lcmd.c
View File

@@ -1199,3 +1199,34 @@ int lushCmd_setenv (lua_State *L) {
}
return 0;
}
/* ===== lush standard library ===== */
static const luaL_Reg lushlib[] = {
{"command", lushCmd_command},
{"interactive", lushCmd_interactive},
{"getenv", lushCmd_getenv},
{"setenv", lushCmd_setenv},
{"subcmd", lushCmd_subcmd},
{NULL, NULL}
};
LUAMOD_API int luaopen_lush (lua_State *L) {
luaL_newlib(L, lushlib);
/* also store integer-keyed entries for OP_LUSH VM access */
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);
/* store in registry for OP_LUSH access */
lua_pushvalue(L, -1);
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
return 1;
}