|
diff --git a/core.c b/core.c
|
| 1 |
#include "luna.h" |
1 |
#include "luna.h" |
| 2 |
|
2 |
|
| 3 |
int main(int argc, char **argv) { |
3 |
typedef struct { |
| 4 |
lua_State *L = luaL_newstate(); |
4 |
ev_timer timer; |
| 5 |
luaL_openlibs(L); |
5 |
lua_State *L; |
| 6 |
|
6 |
int callback_ref; |
| 7 |
// Register core library (timers, metadata) |
7 |
} Timer; |
| 8 |
luaopen_core(L); |
|
|
| 9 |
lua_setglobal(L, "core"); |
|
|
| 10 |
|
|
|
| 11 |
// Register json library |
|
|
| 12 |
luaopen_json(L); |
|
|
| 13 |
lua_setglobal(L, "json"); |
|
|
| 14 |
|
|
|
| 15 |
// Register http library |
|
|
| 16 |
luaopen_http(L); |
|
|
| 17 |
lua_setglobal(L, "http"); |
|
|
| 18 |
|
|
|
| 19 |
// Register request library |
|
|
| 20 |
luaopen_request(L); |
|
|
| 21 |
lua_setglobal(L, "request"); |
|
|
| 22 |
|
|
|
| 23 |
// Register env library |
|
|
| 24 |
luaopen_env(L); |
|
|
| 25 |
lua_setglobal(L, "env"); |
|
|
| 26 |
|
|
|
| 27 |
// Register crypto library |
|
|
| 28 |
luaopen_crypto(L); |
|
|
| 29 |
lua_setglobal(L, "crypto"); |
|
|
| 30 |
|
|
|
| 31 |
// Register sqlite library |
|
|
| 32 |
luaopen_sqlite(L); |
|
|
| 33 |
lua_setglobal(L, "sqlite"); |
|
|
| 34 |
|
|
|
| 35 |
// Register log library |
|
|
| 36 |
luaopen_log(L); |
|
|
| 37 |
lua_setglobal(L, "log"); |
|
|
| 38 |
|
8 |
|
| 39 |
// Register assert library |
9 |
static void timer_cb(struct ev_loop *loop, ev_timer *w, int revents) { |
| 40 |
luaopen_assert(L); |
10 |
Timer *lt = (Timer *)w; |
| 41 |
lua_setglobal(L, "assert"); |
11 |
lua_State *L = lt->L; |
| 42 |
|
12 |
lua_rawgeti(L, LUA_REGISTRYINDEX, lt->callback_ref); |
| 43 |
// Register fs library |
13 |
if (lua_pcall(L, 0, 0, 0) != LUA_OK) { |
| 44 |
luaopen_fs(L); |
14 |
fprintf(stderr, "Error calling timer callback: %s\n", lua_tostring(L, -1)); |
| 45 |
lua_setglobal(L, "fs"); |
15 |
lua_pop(L, 1); |
| 46 |
|
16 |
} |
| 47 |
// Register ini library |
17 |
if (!ev_is_active(w)) { |
| 48 |
luaopen_ini(L); |
18 |
luaL_unref(L, LUA_REGISTRYINDEX, lt->callback_ref); |
| 49 |
lua_setglobal(L, "ini"); |
19 |
free(lt); |
| 50 |
|
20 |
} |
| 51 |
// Register process library |
21 |
} |
| 52 |
luaopen_process(L); |
|
|
| 53 |
lua_setglobal(L, "process"); |
|
|
| 54 |
|
22 |
|
| 55 |
// Register template library |
23 |
static int l_set_timeout(lua_State *L) { |
| 56 |
luaopen_template(L); |
24 |
double delay = luaL_checknumber(L, 1) / 1000.0; |
| 57 |
lua_setglobal(L, "template"); |
25 |
luaL_checktype(L, 2, LUA_TFUNCTION); |
|
|
26 |
Timer *lt = malloc(sizeof(Timer)); |
|
|
27 |
lt->L = L; |
|
|
28 |
lua_pushvalue(L, 2); |
|
|
29 |
lt->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
|
|
30 |
ev_timer_init(<->timer, timer_cb, delay, 0.); |
|
|
31 |
ev_timer_start(EV_DEFAULT, <->timer); |
|
|
32 |
return 0; |
|
|
33 |
} |
| 58 |
|
34 |
|
| 59 |
// Register path library |
35 |
static const struct luaL_Reg core_lib[] = { |
| 60 |
luaopen_path(L); |
36 |
{"set_timeout", l_set_timeout}, |
| 61 |
lua_setglobal(L, "path"); |
37 |
{NULL, NULL} |
|
|
38 |
}; |
| 62 |
|
39 |
|
| 63 |
// Register stash library |
40 |
int luaopen_core(lua_State *L) { |
| 64 |
luaopen_stash(L); |
41 |
luaL_newlib(L, core_lib); |
| 65 |
lua_setglobal(L, "stash"); |
|
|
| 66 |
|
42 |
|
| 67 |
if (argc < 2) { |
43 |
lua_pushstring(L, "0.1.0"); |
| 68 |
fprintf(stderr, "Usage: %s <script.lua>\n", argv[0]); |
44 |
lua_setfield(L, -2, "version"); |
| 69 |
return 1; |
|
|
| 70 |
} |
|
|
| 71 |
|
45 |
|
| 72 |
// Execute lua script |
46 |
#ifdef __linux__ |
| 73 |
if (luaL_dofile(L, argv[1]) != LUA_OK) { |
47 |
lua_pushstring(L, "linux"); |
| 74 |
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); |
48 |
#elif defined(__APPLE__) |
| 75 |
return 1; |
49 |
lua_pushstring(L, "darwin"); |
| 76 |
} |
50 |
#elif defined(_WIN32) |
|
|
51 |
lua_pushstring(L, "windows"); |
|
|
52 |
#else |
|
|
53 |
lua_pushstring(L, "unknown"); |
|
|
54 |
#endif |
|
|
55 |
lua_setfield(L, -2, "platform"); |
| 77 |
|
56 |
|
| 78 |
// Run event loop |
57 |
#if defined(__x86_64__) || defined(_M_X64) |
| 79 |
ev_run(EV_DEFAULT, 0); |
58 |
lua_pushstring(L, "x64"); |
|
|
59 |
#elif defined(__i386__) || defined(_M_IX86) |
|
|
60 |
lua_pushstring(L, "x86"); |
|
|
61 |
#elif defined(__arm__) || defined(_M_ARM) |
|
|
62 |
lua_pushstring(L, "arm"); |
|
|
63 |
#elif defined(__aarch64__) |
|
|
64 |
lua_pushstring(L, "arm64"); |
|
|
65 |
#else |
|
|
66 |
lua_pushstring(L, "unknown"); |
|
|
67 |
#endif |
|
|
68 |
lua_setfield(L, -2, "arch"); |
| 80 |
|
69 |
|
| 81 |
lua_close(L); |
70 |
return 1; |
| 82 |
return 0; |
|
|
| 83 |
} |
71 |
} |
|
diff --git a/main.c b/main.c
|
|
|
1 |
#include "luna.h" |
|
|
2 |
|
|
|
3 |
int main(int argc, char **argv) { |
|
|
4 |
lua_State *L = luaL_newstate(); |
|
|
5 |
luaL_openlibs(L); |
|
|
6 |
|
|
|
7 |
// Register core library (timers, metadata) |
|
|
8 |
luaopen_core(L); |
|
|
9 |
lua_setglobal(L, "core"); |
|
|
10 |
|
|
|
11 |
// Register json library |
|
|
12 |
luaopen_json(L); |
|
|
13 |
lua_setglobal(L, "json"); |
|
|
14 |
|
|
|
15 |
// Register http library |
|
|
16 |
luaopen_http(L); |
|
|
17 |
lua_setglobal(L, "http"); |
|
|
18 |
|
|
|
19 |
// Register request library |
|
|
20 |
luaopen_request(L); |
|
|
21 |
lua_setglobal(L, "request"); |
|
|
22 |
|
|
|
23 |
// Register env library |
|
|
24 |
luaopen_env(L); |
|
|
25 |
lua_setglobal(L, "env"); |
|
|
26 |
|
|
|
27 |
// Register crypto library |
|
|
28 |
luaopen_crypto(L); |
|
|
29 |
lua_setglobal(L, "crypto"); |
|
|
30 |
|
|
|
31 |
// Register sqlite library |
|
|
32 |
luaopen_sqlite(L); |
|
|
33 |
lua_setglobal(L, "sqlite"); |
|
|
34 |
|
|
|
35 |
// Register log library |
|
|
36 |
luaopen_log(L); |
|
|
37 |
lua_setglobal(L, "log"); |
|
|
38 |
|
|
|
39 |
// Register assert library |
|
|
40 |
luaopen_assert(L); |
|
|
41 |
lua_setglobal(L, "assert"); |
|
|
42 |
|
|
|
43 |
// Register fs library |
|
|
44 |
luaopen_fs(L); |
|
|
45 |
lua_setglobal(L, "fs"); |
|
|
46 |
|
|
|
47 |
// Register ini library |
|
|
48 |
luaopen_ini(L); |
|
|
49 |
lua_setglobal(L, "ini"); |
|
|
50 |
|
|
|
51 |
// Register process library |
|
|
52 |
luaopen_process(L); |
|
|
53 |
lua_setglobal(L, "process"); |
|
|
54 |
|
|
|
55 |
// Register template library |
|
|
56 |
luaopen_template(L); |
|
|
57 |
lua_setglobal(L, "template"); |
|
|
58 |
|
|
|
59 |
// Register path library |
|
|
60 |
luaopen_path(L); |
|
|
61 |
lua_setglobal(L, "path"); |
|
|
62 |
|
|
|
63 |
// Register stash library |
|
|
64 |
luaopen_stash(L); |
|
|
65 |
lua_setglobal(L, "stash"); |
|
|
66 |
|
|
|
67 |
if (argc < 2) { |
|
|
68 |
fprintf(stderr, "Usage: %s <script.lua>\n", argv[0]); |
|
|
69 |
return 1; |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
// Execute lua script |
|
|
73 |
if (luaL_dofile(L, argv[1]) != LUA_OK) { |
|
|
74 |
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); |
|
|
75 |
return 1; |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
// Run event loop |
|
|
79 |
ev_run(EV_DEFAULT, 0); |
|
|
80 |
|
|
|
81 |
lua_close(L); |
|
|
82 |
return 0; |
|
|
83 |
} |
|
diff --git a/timer.c b/timer.c
|
| 1 |
#include "luna.h" |
|
|
| 2 |
|
|
|
| 3 |
typedef struct { |
|
|
| 4 |
ev_timer timer; |
|
|
| 5 |
lua_State *L; |
|
|
| 6 |
int callback_ref; |
|
|
| 7 |
} Timer; |
|
|
| 8 |
|
|
|
| 9 |
static void timer_cb(struct ev_loop *loop, ev_timer *w, int revents) { |
|
|
| 10 |
Timer *lt = (Timer *)w; |
|
|
| 11 |
lua_State *L = lt->L; |
|
|
| 12 |
lua_rawgeti(L, LUA_REGISTRYINDEX, lt->callback_ref); |
|
|
| 13 |
if (lua_pcall(L, 0, 0, 0) != LUA_OK) { |
|
|
| 14 |
fprintf(stderr, "Error calling timer callback: %s\n", lua_tostring(L, -1)); |
|
|
| 15 |
lua_pop(L, 1); |
|
|
| 16 |
} |
|
|
| 17 |
if (!ev_is_active(w)) { |
|
|
| 18 |
luaL_unref(L, LUA_REGISTRYINDEX, lt->callback_ref); |
|
|
| 19 |
free(lt); |
|
|
| 20 |
} |
|
|
| 21 |
} |
|
|
| 22 |
|
|
|
| 23 |
static int l_set_timeout(lua_State *L) { |
|
|
| 24 |
double delay = luaL_checknumber(L, 1) / 1000.0; |
|
|
| 25 |
luaL_checktype(L, 2, LUA_TFUNCTION); |
|
|
| 26 |
Timer *lt = malloc(sizeof(Timer)); |
|
|
| 27 |
lt->L = L; |
|
|
| 28 |
lua_pushvalue(L, 2); |
|
|
| 29 |
lt->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
|
|
| 30 |
ev_timer_init(<->timer, timer_cb, delay, 0.); |
|
|
| 31 |
ev_timer_start(EV_DEFAULT, <->timer); |
|
|
| 32 |
return 0; |
|
|
| 33 |
} |
|
|
| 34 |
|
|
|
| 35 |
static const struct luaL_Reg core_lib[] = { |
|
|
| 36 |
{"set_timeout", l_set_timeout}, |
|
|
| 37 |
{NULL, NULL} |
|
|
| 38 |
}; |
|
|
| 39 |
|
|
|
| 40 |
int luaopen_core(lua_State *L) { |
|
|
| 41 |
luaL_newlib(L, core_lib); |
|
|
| 42 |
|
|
|
| 43 |
lua_pushstring(L, "0.1.0"); |
|
|
| 44 |
lua_setfield(L, -2, "version"); |
|
|
| 45 |
|
|
|
| 46 |
#ifdef __linux__ |
|
|
| 47 |
lua_pushstring(L, "linux"); |
|
|
| 48 |
#elif defined(__APPLE__) |
|
|
| 49 |
lua_pushstring(L, "darwin"); |
|
|
| 50 |
#elif defined(_WIN32) |
|
|
| 51 |
lua_pushstring(L, "windows"); |
|
|
| 52 |
#else |
|
|
| 53 |
lua_pushstring(L, "unknown"); |
|
|
| 54 |
#endif |
|
|
| 55 |
lua_setfield(L, -2, "platform"); |
|
|
| 56 |
|
|
|
| 57 |
#if defined(__x86_64__) || defined(_M_X64) |
|
|
| 58 |
lua_pushstring(L, "x64"); |
|
|
| 59 |
#elif defined(__i386__) || defined(_M_IX86) |
|
|
| 60 |
lua_pushstring(L, "x86"); |
|
|
| 61 |
#elif defined(__arm__) || defined(_M_ARM) |
|
|
| 62 |
lua_pushstring(L, "arm"); |
|
|
| 63 |
#elif defined(__aarch64__) |
|
|
| 64 |
lua_pushstring(L, "arm64"); |
|
|
| 65 |
#else |
|
|
| 66 |
lua_pushstring(L, "unknown"); |
|
|
| 67 |
#endif |
|
|
| 68 |
lua_setfield(L, -2, "arch"); |
|
|
| 69 |
|
|
|
| 70 |
return 1; |
|
|
| 71 |
} |
|
|