From 6f8c0f3abda585b3ee1d87e3b8e19c729b52b4a6 Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Mon, 14 Jul 2025 17:51:23 -0500 Subject: Added compat53 support and fixed bugged non-interactive mode --- src/lush.c | 60 ++++++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) (limited to 'src/lush.c') diff --git a/src/lush.c b/src/lush.c index 1c42139..cadc1bc 100644 --- a/src/lush.c +++ b/src/lush.c @@ -21,6 +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 #include #include @@ -1485,13 +1486,14 @@ int main(int argc, char *argv[]) { return 0; } + // init lua state + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + lua_register_api(L); + lua_run_init(L); + // check if being run in command string mode if (argc > 2 && strcmp(argv[1], "-c") == 0) { - // init Lua state - lua_State *L = luaL_newstate(); - luaL_openlibs(L); - lua_register_api(L); - lua_run_init(L); // execute the command provided char *command = argv[2]; @@ -1521,36 +1523,26 @@ int main(int argc, char *argv[]) { return 0; } - // init lua state - lua_State *L = luaL_newstate(); - luaL_openlibs(L); - lua_register_api(L); - lua_run_init(L); + // This is the corrected logic for running a script file non-interactively. + if (argc > 1) { + char *ext = strrchr(argv[1], '.'); + if (ext && strcmp(ext, ".lua") == 0) { + const char *script_name = argv[1]; + // The arguments for the script start at argv[2]. + // We create a pointer to that part of the array. + char **script_args = (argc > 2) ? &argv[2] : NULL; - // if a lua function is passed on load run non-interactively - if (argc > 1) { - char *ext = strrchr(argv[1], '.'); - if (ext) { - ext++; - if (strcmp(ext, "lua") == 0) { - int status = 0; - argv++; - char ***args = lush_split_args(argv, &status); - - if (status == -1) { - fprintf(stderr, "lush: Expected end of quoted string\n"); - } else if (lush_run(L, args, status) != 0) { - exit(1); - } + // Call the script loader directly with the script and its arguments. + if (lua_load_script(L, script_name, script_args) != 0) { + exit(1); // Exit if the script had an error + } - for (int i = 0; args[i]; i++) { - free(args[i]); - } - free(args); - return 0; - } - } - } + lua_close(L); // Clean up and exit + return 0; + } + } + + // --- Interactive Shell Mode --- // eat ^C in main struct sigaction sa_int; @@ -1617,4 +1609,4 @@ int main(int argc, char *argv[]) { if (alt_shell != NULL) free(alt_shell); return 0; -} +} \ No newline at end of file -- cgit v1.2.3-59-g8ed1b From 3961c372ea01f5b9a151bf3ff891602ae0d347d8 Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Tue, 22 Jul 2025 20:23:07 -0500 Subject: Added compat53 to init block: added .gitmodules init to install.sh --- install.sh | 3 +++ src/lush.c | 1 + 2 files changed, 4 insertions(+) (limited to 'src/lush.c') diff --git a/install.sh b/install.sh index ebdbaeb..405cf6e 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,8 @@ #!/bin/sh +echo "Initializing and updating submodules..." +git submodule update --init --recursive + # Function to install packages using apt (Debian/Ubuntu) install_with_apt() { sudo apt-get update diff --git a/src/lush.c b/src/lush.c index cadc1bc..49b205e 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1489,6 +1489,7 @@ int main(int argc, char *argv[]) { // init lua state lua_State *L = luaL_newstate(); luaL_openlibs(L); + luaopen_compat53(L); lua_register_api(L); lua_run_init(L); -- cgit v1.2.3-59-g8ed1b 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(-) (limited to 'src/lush.c') 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 From 69001c115100a19955249bfb6ec45ad332cd5ae8 Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Tue, 22 Jul 2025 23:27:06 -0500 Subject: Compat53 library uses the floor function, which is part of the C math library (libm); removed the incorrect luaopen_compat53 call that was still present --- premake5.lua | 3 ++- src/lush.c | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/lush.c') diff --git a/premake5.lua b/premake5.lua index 20dfa8a..3ec299a 100644 --- a/premake5.lua +++ b/premake5.lua @@ -13,7 +13,8 @@ local lua_lib_path = "/usr/lib" if os.findlib("lua5.4") then lua_inc_path = "/usr/include/lua5.4" lua_lib_path = "/usr/lib/5.4" - links({ "lua5.4" }) +-- Readline for better interactive support, dl for dynamic loading, and m for the math library dependency + links({ "lua5.4", "readline", "dl", "m" }) else links({ "lua" }) end diff --git a/src/lush.c b/src/lush.c index 8beed6a..88d2998 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1510,7 +1510,6 @@ int main(int argc, char *argv[]) { // 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 From 8f1b0212b219aa97116923876adfab5b90f5986a Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Tue, 22 Jul 2025 23:30:27 -0500 Subject: src/lush.c ln:1500-1508: corrected the 'luaL' typos --- src/lush.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/lush.c') diff --git a/src/lush.c b/src/lush.c index 88d2998..82c272e 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1492,12 +1492,12 @@ int main(int argc, char *argv[]) { // init lua state lua_State *L = luaL_newstate(); - luaL_openlibs(L); + lua_openlibs(L); // --- Pre-load compat modules --- // This is to make C modules available to Lua - luaL_getglobal(L, "package"); - luaL_getfield(L, -1, "preload"); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "preload"); // Preload bit32 for Lua 5.1 compatibility lua_pushcfunction(L, luaopen_bit32); -- cgit v1.2.3-59-g8ed1b From 35347bef8d56d9bcd4594d212c9793b8ce59e6ba Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Tue, 22 Jul 2025 23:33:27 -0500 Subject: Re-added luaL_openlibs(L); ln:1495 --- src/lush.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lush.c') diff --git a/src/lush.c b/src/lush.c index 82c272e..eef8f6d 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1492,7 +1492,7 @@ int main(int argc, char *argv[]) { // init lua state lua_State *L = luaL_newstate(); - lua_openlibs(L); + luaL_openlibs(L); // --- Pre-load compat modules --- // This is to make C modules available to Lua -- cgit v1.2.3-59-g8ed1b From 18052989d5722ba9fe01361b53b98330198dbb1e Mon Sep 17 00:00:00 2001 From: Andrew D. France Date: Wed, 23 Jul 2025 01:24:29 -0500 Subject: - 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. --- premake5.lua | 2 +- src/lush.c | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 15 deletions(-) (limited to 'src/lush.c') 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" }) diff --git a/src/lush.c b/src/lush.c index eef8f6d..b512a70 100644 --- a/src/lush.c +++ b/src/lush.c @@ -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); -- cgit v1.2.3-59-g8ed1b