// SPDX-FileCopyrightText: © 2022–2025 Vladimir Zorin // SPDX-License-Identifier: GPL-3.0-or-later #include #include #include #include #include #include #include #include typedef struct mod_lua { const char *const name; const char *const code; const size_t *const size; } mod_lua__t; {{START_CODE}} {{LUAMODS}} {{CLIBS}} void preload_modules(lua_State *const L) { assert(L != NULL); lua_gc(L, LUA_GCSTOP, 0); luaL_openlibs(L); lua_gc(L, LUA_GCRESTART, 0); // Expose the lilush toolchain version so Lua (e.g. lilpack's // min_lilush_version checks) can read the running runtime version. lua_pushstring(L, "{{LILUSH_VERSION}}"); lua_setglobal(L, "LILUSH_VERSION"); lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); luaL_register(L, NULL, c_preload); for (size_t i = 0; lua_preload[i].name != NULL; i++) { int rc = luaL_loadbuffer(L, lua_preload[i].code, *lua_preload[i].size, lua_preload[i].name); if (rc != 0) { const char *err; switch (rc) { case LUA_ERRRUN: err = "runtime error"; break; case LUA_ERRSYNTAX: err = "syntax error"; break; case LUA_ERRMEM: err = "memory error"; break; case LUA_ERRERR: err = "generic error"; break; case LUA_ERRFILE: err = "file error"; break; default: err = "unknown error"; break; } fprintf(stderr, "%s: %s\n", lua_preload[i].name, err); exit(EXIT_FAILURE); } lua_setfield(L, -2, lua_preload[i].name); } }