From 92c575cedf8a40046113516f490a1d5e0a77337e Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Tue, 22 Jul 2025 23:17:16 -0500 Subject: Fixed the header path for compat: preload compat modules for lua within lua-init block --- premake5.lua | 7 ++++++- src/lush.c | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/premake5.lua b/premake5.lua index 127e86e..20dfa8a 100644 --- a/premake5.lua +++ b/premake5.lua @@ -31,7 +31,12 @@ files({ "lib/hashmap/**.h", "lib/hashmap/**.c", "lib/compat53/c-api/compat-5.3.h", - "lib/compat53/c-api/compat-5.3.c" + "lib/compat53/c-api/compat-5.3.c", + "lib/compat53/lbitlib.c", + "lib/compat53/liolib.c", + "lib/compat53/lstrlib.c", + "lib/compat53/ltablib.c", + "lib/compat53/lutf8lib.c" }) defines({ 'LUSH_VERSION="0.3.2"', 'COMPAT53_PREFIX=""' }) diff --git a/src/lush.c b/src/lush.c index 49b205e..8beed6a 100644 --- a/src/lush.c +++ b/src/lush.c @@ -21,7 +21,7 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #include "lua.h" #include "lua_api.h" #include "lualib.h" -#include "../lib/compat53/c-api/compat-5.3.h" +#include "compat-5.3.h" #include #include #include @@ -43,6 +43,10 @@ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. #include #include +// Forward declare the open functions for the compat C modules we need to preload +int luaopen_bit32 (lua_State *L); +int luaopen_utf8 (lua_State *L); + #define BUFFER_SIZE 1024 #define MAX_GLOB 512 @@ -1489,6 +1493,23 @@ int main(int argc, char *argv[]) { // init lua state lua_State *L = luaL_newstate(); luaL_openlibs(L); + + // --- Pre-load compat modules --- + // This is to make C modules available to Lua + luaL_getglobal(L, "package"); + luaL_getfield(L, -1, "preload"); + + // Preload bit32 for Lua 5.1 compatibility + lua_pushcfunction(L, luaopen_bit32); + lua_setfield(L, -2, "bit32"); + + // Preload utf8 for Lua 5.1/5.2 compatibility + lua_pushcfunction(L, luaopen_utf8); + lua_setfield(L, -2, "utf8"); + + // Pop package and preload tables + lua_pop(L, 2); + // --- End pre-loading --- luaopen_compat53(L); lua_register_api(L); lua_run_init(L); -- cgit v1.2.3-59-g8ed1b