diff options
| author | 2025-07-23 01:24:29 -0500 | |
|---|---|---|
| committer | 2025-07-23 01:24:29 -0500 | |
| commit | 18052989d5722ba9fe01361b53b98330198dbb1e (patch) | |
| tree | fbe3bc119b9c2fb9af168c7656ff0d26803b0044 | |
| parent | luaopen_bit32 is called during initialization. This means the bit32 library i... (diff) | |
- The premake5.lua file has been updated to include the LUA_COMPAT_BITLIB compiler definition. This flag instructs the compat-5.3 submodule to build the actual bit32 library instead of a stub that throws a deprecated error.
- The main function in src/lush.c is modified to explicitly load the bit32 and utf8 libraries into the global Lua state at startup using luaL_requiref. This makes them directly accessible to all scripts running in the shell, which is necessary for the test script to find and use the bit32 functions without a require() call.
- Added a null check after luaL_newstate() in src/lush.c to ensure the Lua state is created successfully before its actually used.
| -rw-r--r-- | premake5.lua | 2 | ||||
| -rw-r--r-- | src/lush.c | 23 |
2 files changed, 10 insertions, 15 deletions
diff --git a/premake5.lua b/premake5.lua index 3ec299a..1043c07 100644 --- a/premake5.lua +++ b/premake5.lua @@ -39,7 +39,7 @@ files({ "lib/compat53/ltablib.c", "lib/compat53/lutf8lib.c" }) -defines({ 'LUSH_VERSION="0.3.2"', 'COMPAT53_PREFIX=""' }) +defines({ 'LUSH_VERSION="0.3.2"', 'COMPAT53_PREFIX=""', 'LUA_COMPAT_BITLIB' }) filter("configurations:Debug") defines({ "DEBUG" }) @@ -1492,23 +1492,18 @@ int main(int argc, char *argv[]) { // init lua state lua_State *L = luaL_newstate(); + if (!L) { + fprintf(stderr, "Failed to create Lua state\n"); + return -1; // or handle appropriately + } luaL_openlibs(L); - // --- Pre-load compat modules --- - // This is to make C modules available to Lua - lua_getglobal(L, "package"); - lua_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"); + // --- Load compat modules and make them global --- + luaL_requiref(L, "bit32", luaopen_bit32, 1); + lua_pop(L, 1); // luaL_requiref leaves the module on the stack - // Pop package and preload tables - lua_pop(L, 2); + luaL_requiref(L, "utf8", luaopen_utf8, 1); + lua_pop(L, 1); // --- End pre-loading --- lua_register_api(L); lua_run_init(L); |
