loader: Move to using linker sets to bring in optional bits

The graphics stuff is optional. When it is pulled into the system, we
use a linker set to initialize the lua bindings for it now.

Sponsored by:		Netflix
Reviewed by:		kevans, jhb
Differential Revision:	https://reviews.freebsd.org/D43906
This commit is contained in:
Warner Losh 2024-02-15 20:53:47 -07:00
parent 23d9b5c9fe
commit 0921a771da
3 changed files with 39 additions and 26 deletions

View file

@ -97,31 +97,6 @@ static const luaL_Reg loadedlibs[] = {
{NULL, NULL}
};
static void
interp_init_md(lua_State *L)
{
luaL_requiref(L, "gfx", luaopen_gfx, 1);
lua_pop(L, 1); /* Remove lib */
/*
* Add in the comparability references in the loader table. Doing it with
* a pseudo-embedded script is easier than the raw calls.
*/
if (luaL_dostring(L,
"loader.fb_bezier = gfx.fb_bezier\n"
"loader.fb_drawrect = gfx.fb_drawrect\n"
"loader.fb_line = gfx.fb_line\n"
"loader.fb_putimage = gfx.fb_putimage\n"
"loader.fb_setpixel = gfx.fb_setpixel\n"
"loader.term_drawrect = gfx.term_drawrect\n"
"loader.term_putimage = gfx.term_putimage") != 0) {
lua_pop(L, 1);
const char *errstr = lua_tostring(L, -1);
errstr = errstr == NULL ? "unknown" : errstr;
printf("Error adding compat loader bindings: %s.\n", errstr);
}
}
void
interp_init(void)
{
@ -129,6 +104,7 @@ interp_init(void)
struct interp_lua_softc *softc = &lua_softc;
const char *filename;
const luaL_Reg *lib;
lua_init_md_t **fnpp;
TSENTER();
@ -148,7 +124,8 @@ interp_init(void)
lua_pop(luap, 1); /* remove lib */
}
interp_init_md(luap);
LUA_FOREACH_SET(fnpp)
(*fnpp)(luap);
filename = getenv("loader_lua");
if (filename == NULL)

View file

@ -245,3 +245,30 @@ void
gfx_interp_md(void)
{
}
static void
gfx_init_md(lua_State *L)
{
luaL_requiref(L, "gfx", luaopen_gfx, 1);
lua_pop(L, 1); /* Remove lib */
/*
* Add in the compatibility references in the loader table. Doing it with
* a pseudo-embedded script is easier than the raw calls.
*/
if (luaL_dostring(L,
"loader.fb_bezier = gfx.fb_bezier\n"
"loader.fb_drawrect = gfx.fb_drawrect\n"
"loader.fb_line = gfx.fb_line\n"
"loader.fb_putimage = gfx.fb_putimage\n"
"loader.fb_setpixel = gfx.fb_setpixel\n"
"loader.term_drawrect = gfx.term_drawrect\n"
"loader.term_putimage = gfx.term_putimage") != 0) {
lua_pop(L, 1);
const char *errstr = lua_tostring(L, -1);
errstr = errstr == NULL ? "unknown" : errstr;
printf("Error adding compat loader bindings: %s.\n", errstr);
}
}
LUA_COMPILE_SET(gfx_init_md);

View file

@ -30,3 +30,12 @@ int luaopen_gfx(lua_State *);
int luaopen_loader(lua_State *);
int luaopen_io(lua_State *);
int luaopen_pager(lua_State *);
#include <sys/linker_set.h>
typedef void lua_init_md_t(lua_State *);
#define LUA_COMPILE_SET(func) \
DATA_SET(Xficl_compile_set, func) /* XXX linker set know by ldscrips */
#define LUA_FOREACH_SET(s) \
SET_FOREACH((s), Xficl_compile_set)
SET_DECLARE(Xficl_compile_set, lua_init_md_t);