diff --git a/build.sh b/build.sh index 5391663..e5c87a7 100755 --- a/build.sh +++ b/build.sh @@ -24,12 +24,12 @@ # Default env vars -if [ -z "$debug" ]; then debug=0; fi -if [ -z "$debug_coverage" ]; then debug_coverage=0; fi -if [ -z "$GCC" ]; then GCC="gcc"; fi -if [ -z "$AR" ]; then AR="ar"; fi -if [ -z "$MAKE" ]; then MAKE="make"; fi -if [ -z "$GCC_VER" ]; then GCC_VER=gnu99; fi +if [ -z "$debug" ]; then debug=0; fi +if [ -z "$debug_coverage" ]; then debug_coverage=0; fi +if [ -z "$GCC" ]; then GCC="gcc"; fi +if [ -z "$AR" ]; then AR="ar"; fi +if [ -z "$MAKE" ]; then MAKE="make"; fi +if [ -z "$GCC_VER" ]; then GCC_VER=gnu99; fi # On help message request diff --git a/src/darr.c b/src/darr.c deleted file mode 100644 index ac282e9..0000000 --- a/src/darr.c +++ /dev/null @@ -1,40 +0,0 @@ -/* MIT License - * - * Copyright (c) 2017-2019 Cody Tilkins - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "darr.h" - -Array* array_new(size_t init_size, size_t increment, size_t type_size) { - Array* arr = malloc(sizeof(Array)); - if(arr == 0) - return 0; - arr->size = 0; - arr->cap = init_size; - arr->increment = increment; - arr->type_size = type_size; - arr->data = malloc(init_size * sizeof(void*)); - if(arr->data == 0) { - free(arr); - return 0; - } - return arr; -} diff --git a/src/darr.h b/src/darr.h index cc02a7b..60129a5 100644 --- a/src/darr.h +++ b/src/darr.h @@ -26,6 +26,7 @@ #include #include + typedef struct tagArray { size_t size; size_t cap; @@ -35,6 +36,30 @@ typedef struct tagArray { } Array; + +static inline Array* array_new(size_t init_size, size_t increment, size_t type_size) { + Array* arr = malloc(sizeof(Array)); + if(arr == 0) + return 0; + arr->size = 0; + arr->cap = init_size; + arr->increment = increment; + arr->type_size = type_size; + arr->data = malloc(init_size * sizeof(void*)); + if(arr->data == 0) { + free(arr); + return 0; + } + return arr; +} + +static inline void array_free(void* arr) { + Array* a = (Array*) arr; + free(a->data); + free(a); +} + + static int array_ensure_size(Array* arr, size_t size) { if(arr->size + size >= arr->cap) { arr->data = realloc(arr->data, (arr->cap + arr->increment) * sizeof(void*)); @@ -46,15 +71,6 @@ static int array_ensure_size(Array* arr, size_t size) { } -Array* array_new(size_t init_size, size_t increment, size_t type_size); - -static inline void array_free(void* arr) { - Array* a = (Array*) arr; - free(a->data); - free(a); -} - - static inline int array_push(Array* arr, void* data) { if(array_ensure_size(arr, 1) == 0) return 0; diff --git a/src/jitsupport.c b/src/jitsupport.c index 1c3d77d..1b67c08 100644 --- a/src/jitsupport.c +++ b/src/jitsupport.c @@ -25,157 +25,159 @@ # include "jitsupport.h" -// LuaJIT functions slightly modified for LuaConsole -// TODO: optimize + // LuaJIT functions slightly modified for LuaConsole + // TODO: optimize -// Load add-on module -int loadjitmodule(lua_State* L) { - lua_getglobal(L, "require"); - lua_pushliteral(L, "jit."); - lua_pushvalue(L, -3); - lua_concat(L, 2); - if (lua_pcall(L, 1, 1, 0)) { - const char *msg = lua_tostring(L, -1); - if (msg && !strncmp(msg, "module ", 7)) - goto nomodule; - msg = lua_tostring(L, -1); - if(msg == NULL) msg = "(error object is not a string)"; - fprintf(stderr, "LuaJIT Error: %s\n", msg); - lua_pop(L, 1); - return 1; - } - lua_getfield(L, -1, "start"); - if (lua_isnil(L, -1)) { -nomodule: - fputs("LuaJIT Error: unknown luaJIT command or jit.* modules not installed\n", stderr); - return 1; + // Load add-on module + int loadjitmodule(lua_State* L) { + lua_getglobal(L, "require"); + lua_pushliteral(L, "jit."); + lua_pushvalue(L, -3); + lua_concat(L, 2); + if (lua_pcall(L, 1, 1, 0)) { + const char *msg = lua_tostring(L, -1); + if (msg && !strncmp(msg, "module ", 7)) + goto nomodule; + msg = lua_tostring(L, -1); + if(msg == NULL) msg = "(error object is not a string)"; + fprintf(stderr, "LuaJIT Error: %s\n", msg); + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -1, "start"); + if (lua_isnil(L, -1)) { + nomodule: + fputs("LuaJIT Error: unknown luaJIT command or jit.* modules not installed\n", stderr); + return 1; + } + lua_remove(L, -2); // Drop module table + return 0; } - lua_remove(L, -2); // Drop module table - return 0; -} -// Run command with options -int runcmdopt(lua_State* L, const char* opt) { - int narg = 0; - if (opt && *opt) { - for (;;) { // Split arguments - const char *p = strchr(opt, ','); - narg++; - if (!p) break; - if (p == opt) - lua_pushnil(L); + // Run command with options + int runcmdopt(lua_State* L, const char* opt) { + int narg = 0; + if (opt && *opt) { + for (;;) { // Split arguments + const char *p = strchr(opt, ','); + narg++; + if (!p) break; + if (p == opt) + lua_pushnil(L); + else + lua_pushlstring(L, opt, (size_t)(p - opt)); + opt = p + 1; + } + if (*opt) + lua_pushstring(L, opt); else - lua_pushlstring(L, opt, (size_t)(p - opt)); - opt = p + 1; + lua_pushnil(L); + } + int status = 0; + if((status = lua_pcall(L, narg, 0, 0)) != 0) { + const char* msg = lua_tostring(L, -1); + if(msg == NULL) msg = "(error object is not a string)"; + fprintf(stderr, "LuaJIT Error: %s\n", msg); + lua_pop(L, 1); } - if (*opt) - lua_pushstring(L, opt); - else - lua_pushnil(L); + return status; } - int status = 0; - if((status = lua_pcall(L, narg, 0, 0)) != 0) { - const char* msg = lua_tostring(L, -1); - if(msg == NULL) msg = "(error object is not a string)"; - fprintf(stderr, "LuaJIT Error: %s\n", msg); - lua_pop(L, 1); + + // JIT engine control command: try jit library first or load add-on module + int dojitcmd(lua_State* L, const char* cmd) { + const char *opt = strchr(cmd, '='); + lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, "jit"); // Get jit.* module table + lua_remove(L, -2); + lua_pushvalue(L, -2); + lua_gettable(L, -2); // Lookup library function + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); // Drop non-function and jit.* table, keep module name + if (loadjitmodule(L)) { + return 1; + } + } else + lua_remove(L, -2); // Drop jit.* table + lua_remove(L, -2); // Drop module name + return runcmdopt(L, opt ? opt+1 : opt); + } + + // Optimization flags + int dojitopt(lua_State* L, const char* opt) { + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, "jit.opt"); // Get jit.opt.* module table + lua_remove(L, -2); + lua_getfield(L, -1, "start"); + lua_remove(L, -2); + return runcmdopt(L, opt); } - return status; -} -// JIT engine control command: try jit library first or load add-on module -int dojitcmd(lua_State* L, const char* cmd) { - const char *opt = strchr(cmd, '='); - lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit"); // Get jit.* module table - lua_remove(L, -2); - lua_pushvalue(L, -2); - lua_gettable(L, -2); // Lookup library function - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); // Drop non-function and jit.* table, keep module name - if (loadjitmodule(L)) { + // Save or list bytecode + int dobytecode(lua_State* L, char** argv) { + int narg = 0; + lua_pushliteral(L, "bcsave"); + if (loadjitmodule(L)) return 1; + if (argv[0][2]) { + narg++; + argv[0][1] = '-'; + lua_pushstring(L, argv[0]+1); } - } else - lua_remove(L, -2); // Drop jit.* table - lua_remove(L, -2); // Drop module name - return runcmdopt(L, opt ? opt+1 : opt); -} - -// Optimization flags -int dojitopt(lua_State* L, const char* opt) { - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit.opt"); // Get jit.opt.* module table - lua_remove(L, -2); - lua_getfield(L, -1, "start"); - lua_remove(L, -2); - return runcmdopt(L, opt); -} - -// Save or list bytecode -int dobytecode(lua_State* L, char** argv) { - int narg = 0; - lua_pushliteral(L, "bcsave"); - if (loadjitmodule(L)) - return 1; - if (argv[0][2]) { - narg++; - argv[0][1] = '-'; - lua_pushstring(L, argv[0]+1); - } - for (argv++; *argv != NULL; narg++, argv++) - lua_pushstring(L, *argv); - int status = 0; - if((status = lua_pcall(L, narg, 0, 0)) != 0) { - const char* msg = lua_tostring(L, -1); - if(msg == NULL) msg = "(error object is not a string)"; - fprintf(stderr, "LuaJIT Error: %s\n", msg); - lua_pop(L, 1); + for (argv++; *argv != NULL; narg++, argv++) + lua_pushstring(L, *argv); + int status = 0; + if((status = lua_pcall(L, narg, 0, 0)) != 0) { + const char* msg = lua_tostring(L, -1); + if(msg == NULL) msg = "(error object is not a string)"; + fprintf(stderr, "LuaJIT Error: %s\n", msg); + lua_pop(L, 1); + } + return status; } - return status; -} -// Prints JIT settings -void print_jit_status(lua_State* L) { - lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); - lua_getfield(L, -1, "jit"); - lua_remove(L, -2); - lua_getfield(L, -1, "status"); - lua_remove(L, -2); // _LOADED.jit.status - int n = lua_gettop(L); - lua_call(L, 0, LUA_MULTRET); - fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout); - const char* s = NULL; - for (n++; (s = lua_tostring(L, n)); n++) { - putc(' ', stdout); - fputs(s, stdout); + // Prints JIT settings + void print_jit_status(lua_State* L) { + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, "jit"); + lua_remove(L, -2); + lua_getfield(L, -1, "status"); + lua_remove(L, -2); // _LOADED.jit.status + int n = lua_gettop(L); + lua_call(L, 0, LUA_MULTRET); + fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout); + const char* s = NULL; + for (n++; (s = lua_tostring(L, n)); n++) { + putc(' ', stdout); + fputs(s, stdout); + } + putc('\n', stdout); + lua_pop(L, lua_gettop(L) - n); } - putc('\n', stdout); - lua_pop(L, lua_gettop(L) - n); -} -int jitargs(lua_State* L, Array* luajit_jcmds, Array* luajit_opts, char** luajit_bc, int squelch, int post_exist) { - if(luajit_jcmds != NULL) { - for(size_t i=0; isize; i++) - if(dojitcmd(L, (const char*) array_get(luajit_jcmds, i)) != 0) - fputs("LuaJIT Warning: Failed to execute control command or load extension module!\n", stderr); - array_free(luajit_jcmds); - } - if(luajit_opts != NULL) { - for(size_t i=0; isize; i++) - if(dojitopt(L, (const char*) array_get(luajit_opts, i)) != 0) - fputs("LuaJIT Warning: Failed to set with -O!\n", stderr); - array_free(luajit_opts); + int jitargs(lua_State* L, Array* luajit_jcmds, Array* luajit_opts, char** luajit_bc, int squelch, int post_exist) { + if(luajit_jcmds != NULL) { + for(size_t i=0; isize; i++) + if(dojitcmd(L, (const char*) array_get(luajit_jcmds, i)) != 0) + fputs("LuaJIT Warning: Failed to execute control command or load extension module!\n", stderr); + array_free(luajit_jcmds); + } + + if(luajit_opts != NULL) { + for(size_t i=0; isize; i++) + if(dojitopt(L, (const char*) array_get(luajit_opts, i)) != 0) + fputs("LuaJIT Warning: Failed to set with -O!\n", stderr); + array_free(luajit_opts); + } + + if(squelch == 0 && post_exist == 1) + print_jit_status(L); + + if(luajit_bc != NULL) + return dobytecode(L, luajit_bc); + return 0; // success } - - if(squelch == 0 && post_exist == 1) - print_jit_status(L); - - if(luajit_bc != NULL) - return dobytecode(L, luajit_bc); - return 0; // success -} -#endif + +#endif // EOF if defined(LUA_JIT_51) diff --git a/src/jitsupport.h b/src/jitsupport.h index 107451b..75c54eb 100644 --- a/src/jitsupport.h +++ b/src/jitsupport.h @@ -31,13 +31,14 @@ # include "darr.h" -int loadjitmodule(lua_State* L); -int runcmdopt(lua_State* L, const char* opt); -int dojitcmd(lua_State* L, const char* cmd); -int dojitopt(lua_State* L, const char* opt); -int dobytecode(lua_State* L, char** argv); -void print_jit_status(lua_State* L); -int jitargs(lua_State* L, Array* luajit_jcmds, Array* luajit_opts, char** luajit_bc, int squelch, int post_exist); + int loadjitmodule(lua_State* L); + int runcmdopt(lua_State* L, const char* opt); + int dojitcmd(lua_State* L, const char* cmd); + int dojitopt(lua_State* L, const char* opt); + int dobytecode(lua_State* L, char** argv); + void print_jit_status(lua_State* L); -#endif + int jitargs(lua_State* L, Array* luajit_jcmds, Array* luajit_opts, char** luajit_bc, int squelch, int post_exist); + +#endif // EOF if defined(LUA_JIT_51) diff --git a/src/ldata.c b/src/ldata.c index 2121966..2610234 100644 --- a/src/ldata.c +++ b/src/ldata.c @@ -24,8 +24,8 @@ // environment variable for lua usage // TODO: I want to support LUA_INIT_5_2 LUA_INIT_5_1 and LUA_INIT_5_3 (ENV_VAR_EXT) which version takes precedence and falls back to LUA_INIT afterward -#define ENV_VAR "LUA_INIT" // #define ENV_VAR_EXT (0) +#define ENV_VAR "LUA_INIT" #include diff --git a/src/luadriver.c b/src/luadriver.c index b840dac..98940cb 100644 --- a/src/luadriver.c +++ b/src/luadriver.c @@ -22,7 +22,7 @@ */ #if !defined(DEFAULT_LUA) -# if defined(_WIN32) || defineD(_WIN64) +# if defined(_WIN32) || defined(_WIN64) # define DEFAULT_LUA "lclua-5.3.5.dll" # else # define DEFAULT_LUA "lclua-5.3.5.so" @@ -83,7 +83,6 @@ int main(int argc, char** argv) { if(argc < 2 || (argv[1][0] == '-' || argv[1][0] == '/')) { // don't try to execute file if it isn't first argument ARGS.post_exist = 1; } else { - // i