diff --git a/Makefile b/Makefile index 0242c381bd804f23f186636955ab984b7c64fe16..031d9666c55d019966a184cee2745e1c191c86a4 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = zig cc -target x86_64-linux-musl CFLAGS = -Wall -O2 -Iluajit/src -Ilibev -Icjson -Icurl/include -Iwolfssl -Iwolfssl/build -Isqlite -Ijinjac/libjinjac/include -Ijinjac/libjinjac/src -Iiniparser/src -DCURL_STATICLIB LIBS = luajit/src/libluajit.a libev/.libs/libev.a curl/build/lib/libcurl.a wolfssl/build/libwolfssl.a jinjac/build/libjinjac/src/liblibjinjac_static.a iniparser/build/libiniparser.a -lm -ldl -lpthread -lunwind -SRCS = core.c json.c http.c timer.c util.c log.c assert.c request.c env.c crypto.c sqlite.c fs.c process.c template.c ini.c path.c stash.c sqlite/sqlite3.c cjson/cJSON.c +SRCS = main.c core.c json.c http.c util.c log.c assert.c request.c env.c crypto.c sqlite.c fs.c process.c template.c ini.c path.c stash.c sqlite/sqlite3.c cjson/cJSON.c OBJS = $(SRCS:.c=.o) MEX_ASSURE = zig make cmake flex bison diff --git a/core.c b/core.c index d3ef2bdb061de8c8026f952989034dc7b338b493..b78f16410defd2ff8cf784d423cdfe6c4d92262c 100644 --- a/core.c +++ b/core.c @@ -1,83 +1,71 @@ #include "luna.h" -int main(int argc, char **argv) { - lua_State *L = luaL_newstate(); - luaL_openlibs(L); - - // Register core library (timers, metadata) - luaopen_core(L); - lua_setglobal(L, "core"); - - // Register json library - luaopen_json(L); - lua_setglobal(L, "json"); - - // Register http library - luaopen_http(L); - lua_setglobal(L, "http"); - - // Register request library - luaopen_request(L); - lua_setglobal(L, "request"); - - // Register env library - luaopen_env(L); - lua_setglobal(L, "env"); - - // Register crypto library - luaopen_crypto(L); - lua_setglobal(L, "crypto"); - - // Register sqlite library - luaopen_sqlite(L); - lua_setglobal(L, "sqlite"); - - // Register log library - luaopen_log(L); - lua_setglobal(L, "log"); +typedef struct { + ev_timer timer; + lua_State *L; + int callback_ref; +} Timer; - // Register assert library - luaopen_assert(L); - lua_setglobal(L, "assert"); - - // Register fs library - luaopen_fs(L); - lua_setglobal(L, "fs"); - - // Register ini library - luaopen_ini(L); - lua_setglobal(L, "ini"); - - // Register process library - luaopen_process(L); - lua_setglobal(L, "process"); +static void timer_cb(struct ev_loop *loop, ev_timer *w, int revents) { + Timer *lt = (Timer *)w; + lua_State *L = lt->L; + lua_rawgeti(L, LUA_REGISTRYINDEX, lt->callback_ref); + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + fprintf(stderr, "Error calling timer callback: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + } + if (!ev_is_active(w)) { + luaL_unref(L, LUA_REGISTRYINDEX, lt->callback_ref); + free(lt); + } +} - // Register template library - luaopen_template(L); - lua_setglobal(L, "template"); +static int l_set_timeout(lua_State *L) { + double delay = luaL_checknumber(L, 1) / 1000.0; + luaL_checktype(L, 2, LUA_TFUNCTION); + Timer *lt = malloc(sizeof(Timer)); + lt->L = L; + lua_pushvalue(L, 2); + lt->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); + ev_timer_init(<->timer, timer_cb, delay, 0.); + ev_timer_start(EV_DEFAULT, <->timer); + return 0; +} - // Register path library - luaopen_path(L); - lua_setglobal(L, "path"); +static const struct luaL_Reg core_lib[] = { + {"set_timeout", l_set_timeout}, + {NULL, NULL} +}; - // Register stash library - luaopen_stash(L); - lua_setglobal(L, "stash"); +int luaopen_core(lua_State *L) { + luaL_newlib(L, core_lib); - if (argc < 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return 1; - } + lua_pushstring(L, "0.1.0"); + lua_setfield(L, -2, "version"); - // Execute lua script - if (luaL_dofile(L, argv[1]) != LUA_OK) { - fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); - return 1; - } +#ifdef __linux__ + lua_pushstring(L, "linux"); +#elif defined(__APPLE__) + lua_pushstring(L, "darwin"); +#elif defined(_WIN32) + lua_pushstring(L, "windows"); +#else + lua_pushstring(L, "unknown"); +#endif + lua_setfield(L, -2, "platform"); - // Run event loop - ev_run(EV_DEFAULT, 0); +#if defined(__x86_64__) || defined(_M_X64) + lua_pushstring(L, "x64"); +#elif defined(__i386__) || defined(_M_IX86) + lua_pushstring(L, "x86"); +#elif defined(__arm__) || defined(_M_ARM) + lua_pushstring(L, "arm"); +#elif defined(__aarch64__) + lua_pushstring(L, "arm64"); +#else + lua_pushstring(L, "unknown"); +#endif + lua_setfield(L, -2, "arch"); - lua_close(L); - return 0; + return 1; } diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..d3ef2bdb061de8c8026f952989034dc7b338b493 --- /dev/null +++ b/main.c @@ -0,0 +1,83 @@ +#include "luna.h" + +int main(int argc, char **argv) { + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + // Register core library (timers, metadata) + luaopen_core(L); + lua_setglobal(L, "core"); + + // Register json library + luaopen_json(L); + lua_setglobal(L, "json"); + + // Register http library + luaopen_http(L); + lua_setglobal(L, "http"); + + // Register request library + luaopen_request(L); + lua_setglobal(L, "request"); + + // Register env library + luaopen_env(L); + lua_setglobal(L, "env"); + + // Register crypto library + luaopen_crypto(L); + lua_setglobal(L, "crypto"); + + // Register sqlite library + luaopen_sqlite(L); + lua_setglobal(L, "sqlite"); + + // Register log library + luaopen_log(L); + lua_setglobal(L, "log"); + + // Register assert library + luaopen_assert(L); + lua_setglobal(L, "assert"); + + // Register fs library + luaopen_fs(L); + lua_setglobal(L, "fs"); + + // Register ini library + luaopen_ini(L); + lua_setglobal(L, "ini"); + + // Register process library + luaopen_process(L); + lua_setglobal(L, "process"); + + // Register template library + luaopen_template(L); + lua_setglobal(L, "template"); + + // Register path library + luaopen_path(L); + lua_setglobal(L, "path"); + + // Register stash library + luaopen_stash(L); + lua_setglobal(L, "stash"); + + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + // Execute lua script + if (luaL_dofile(L, argv[1]) != LUA_OK) { + fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); + return 1; + } + + // Run event loop + ev_run(EV_DEFAULT, 0); + + lua_close(L); + return 0; +} diff --git a/timer.c b/timer.c deleted file mode 100644 index b78f16410defd2ff8cf784d423cdfe6c4d92262c..0000000000000000000000000000000000000000 --- a/timer.c +++ /dev/null @@ -1,71 +0,0 @@ -#include "luna.h" - -typedef struct { - ev_timer timer; - lua_State *L; - int callback_ref; -} Timer; - -static void timer_cb(struct ev_loop *loop, ev_timer *w, int revents) { - Timer *lt = (Timer *)w; - lua_State *L = lt->L; - lua_rawgeti(L, LUA_REGISTRYINDEX, lt->callback_ref); - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - fprintf(stderr, "Error calling timer callback: %s\n", lua_tostring(L, -1)); - lua_pop(L, 1); - } - if (!ev_is_active(w)) { - luaL_unref(L, LUA_REGISTRYINDEX, lt->callback_ref); - free(lt); - } -} - -static int l_set_timeout(lua_State *L) { - double delay = luaL_checknumber(L, 1) / 1000.0; - luaL_checktype(L, 2, LUA_TFUNCTION); - Timer *lt = malloc(sizeof(Timer)); - lt->L = L; - lua_pushvalue(L, 2); - lt->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - ev_timer_init(<->timer, timer_cb, delay, 0.); - ev_timer_start(EV_DEFAULT, <->timer); - return 0; -} - -static const struct luaL_Reg core_lib[] = { - {"set_timeout", l_set_timeout}, - {NULL, NULL} -}; - -int luaopen_core(lua_State *L) { - luaL_newlib(L, core_lib); - - lua_pushstring(L, "0.1.0"); - lua_setfield(L, -2, "version"); - -#ifdef __linux__ - lua_pushstring(L, "linux"); -#elif defined(__APPLE__) - lua_pushstring(L, "darwin"); -#elif defined(_WIN32) - lua_pushstring(L, "windows"); -#else - lua_pushstring(L, "unknown"); -#endif - lua_setfield(L, -2, "platform"); - -#if defined(__x86_64__) || defined(_M_X64) - lua_pushstring(L, "x64"); -#elif defined(__i386__) || defined(_M_IX86) - lua_pushstring(L, "x86"); -#elif defined(__arm__) || defined(_M_ARM) - lua_pushstring(L, "arm"); -#elif defined(__aarch64__) - lua_pushstring(L, "arm64"); -#else - lua_pushstring(L, "unknown"); -#endif - lua_setfield(L, -2, "arch"); - - return 1; -}