loader: separate lang init from scripting init

Create interp_preinit() to initialize the scripting language to run
scripts. Make sure you can call it multiple times, but only the first
one has effect, After it's call, you can run scripts in the scripting
language. At the moment, no functional change.

Sponsored by:		Netflix
This commit is contained in:
Warner Losh 2024-05-19 11:48:14 -06:00
parent 1d7bdae9ca
commit 68344c9c6c
5 changed files with 41 additions and 6 deletions

View File

@ -54,7 +54,8 @@ bool interp_has_builtin_cmd(const char *cmd);
/* Called by interp.c for interp_*.c embedded interpreters */
int interp_include(const char *); /* Execute commands from filename */
void interp_init(void); /* Initialize interpreater */
void interp_preinit(void); /* Initialize interpreater execution engine */
void interp_init(void); /* Initialize interpreater and run main script */
int interp_run(const char *); /* Run a single command */
/* interp_backslash.c */

View File

@ -60,6 +60,7 @@ interact(void)
* we need to switch interpreters.
*/
interp_identifier = bootprog_interp;
interp_preinit();
interp_init();
printf("\n");

View File

@ -337,12 +337,21 @@ bf_run(const char *line)
return (result);
}
static bool preinit_run = false;
void
interp_preinit(void)
{
if (preinit_run)
return;
setenv("script.lang", "forth", 1);
bf_init();
preinit_run = true;
}
void
interp_init(void)
{
setenv("script.lang", "forth", 1);
bf_init();
/* Read our default configuration. */
interp_include("/boot/loader.rc");
}

View File

@ -96,17 +96,21 @@ static const luaL_Reg loadedlibs[] = {
{NULL, NULL}
};
static bool preinit_done = false;
void
interp_init(void)
interp_preinit(void)
{
lua_State *luap;
struct interp_lua_softc *softc = &lua_softc;
const char *filename;
const luaL_Reg *lib;
lua_init_md_t **fnpp;
TSENTER();
if (preinit_done)
return;
setenv("script.lang", "lua", 1);
LDBG("creating context");
@ -126,6 +130,21 @@ interp_init(void)
LUA_FOREACH_SET(fnpp)
(*fnpp)(luap);
preinit_done = true;
TSEXIT();
}
void
interp_init(void)
{
lua_State *luap;
struct interp_lua_softc *softc = &lua_softc;
const char *filename;
TSENTER();
luap = softc->luap;
filename = getenv("loader_lua");
if (filename == NULL)
filename = LOADER_LUA;

View File

@ -34,6 +34,11 @@
INTERP_DEFINE("simp");
void
interp_preinit(void)
{
}
void
interp_init(void)
{