mooa

Lua + lubev + sandboxing
git clone https://code.literati.org/mooa.git
Log | Files | Refs | README | LICENSE

commit 0017c88b88cefa6bfa11f1055afa3a28723c3997
parent 20511b886183e9e697ab8d22d7bd199807afba9f
Author: Sean Lynch <seanl@literati.org>
Date:   Wed, 25 Jun 2014 21:48:01 -0700

Use the libev default loop

Diffstat:
Mdns.c | 21++++++++++++---------
Mtask.c | 19++++---------------
Mtask.h | 3---
Mutils.c | 17++++++++---------
4 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/dns.c b/dns.c @@ -10,6 +10,7 @@ #include <udns.h> #include "task.h" +#include "dns.h" typedef struct mooa_dns_context { struct mooa_dns_context *next; @@ -28,29 +29,31 @@ typedef struct mooa_dns_state { static void mooa_dns_timer_cb(struct ev_loop *loop, ev_timer *timer, int revents) { + (void)loop; + (void)revents; mooa_dns_context_t *context = timer->data; - dns_timeouts(context->ctx, -1, - (time_t)ev_now(mooa_task_get_loop(context->coro))); + dns_timeouts(context->ctx, -1, 0); } static void mooa_dns_io_cb(struct ev_loop *loop, ev_io *io, int revents) { + (void)loop; + (void)revents; mooa_dns_context_t *context = io->data; - dns_ioevent(context->ctx, (time_t)ev_now(loop)); + dns_ioevent(context->ctx, 0); } static void mooa_dns_utm_cb(struct dns_ctx *ctx, int timeout, void *data) { - struct ev_loop *loop; + struct ev_loop *loop = EV_DEFAULT; mooa_dns_context_t *context; if (ctx == NULL) { return; } context = data; - loop = mooa_task_get_loop(context->coro); ev_timer_stop(loop, &context->timer); if (timeout >= 0.0) { ev_timer_set(&context->timer, timeout, 0.0); @@ -117,7 +120,7 @@ static mooa_dns_context_t *mooa_dns_get_context(lua_State *L) { static int mooa_dns_state_finalize(lua_State *L) { mooa_dns_state_t *state = lua_touserdata(L, 1); - struct ev_loop *loop = mooa_task_get_loop(L); + struct ev_loop *loop = EV_DEFAULT; mooa_dns_context_t *context; for (context = state->free_contexts; @@ -151,9 +154,9 @@ static void mooa_dns_gethostbyname_cb(struct dns_ctx *ctx, int i; mooa_dns_context_t *context = data; lua_State *L = context->coro; - struct ev_loop *loop = mooa_task_get_loop(L); + struct ev_loop *loop = EV_DEFAULT; char addr[INET_ADDRSTRLEN]; - char *error; + const char *error; ev_io_stop(loop, &context->io); @@ -222,7 +225,7 @@ static int mooa_dns_gethostbyname(lua_State *L) { dns_submit_a4(context->ctx, name, 0, mooa_dns_gethostbyname_cb, context); ev_io_set(&context->io, dns_sock(context->ctx), EV_READ); - ev_io_start(mooa_task_get_loop(L), &context->io); + ev_io_start(EV_DEFAULT, &context->io); return lua_yield(L, 0); } diff --git a/task.c b/task.c @@ -71,17 +71,6 @@ static mooa_task_t *mooa_task_get(lua_State *L) { } -/* Get the libev loop */ -struct ev_loop *mooa_task_get_loop(lua_State *L) { - struct ev_loop *loop; - lua_getfield(L, LUA_REGISTRYINDEX, "mooa_task_loop"); - loop = lua_touserdata(L, -1); - assert(loop != NULL); - lua_pop(L, 1); - return loop; -} - - static void mooa_task_schedule(mooa_task_t *task, int nargs) { if (task->coro == NULL) { warnx("Attempt to schedule task %p which is dead", task); @@ -111,7 +100,7 @@ void mooa_task_wake(lua_State *L, int nargs) { void mooa_task_wait_for_read(lua_State *L, int fd) { mooa_task_t *task = mooa_task_get(L); ev_io_set(&task->io, fd, EV_READ); - ev_io_start(mooa_task_get_loop(L), &task->io); + ev_io_start(EV_DEFAULT, &task->io); } @@ -119,7 +108,7 @@ void mooa_task_wait_for_read(lua_State *L, int fd) { void mooa_task_wait_for_write(lua_State *L, int fd) { mooa_task_t *task = mooa_task_get(L); ev_io_set(&task->io, fd, EV_WRITE); - ev_io_start(mooa_task_get_loop(L), &task->io); + ev_io_start(EV_DEFAULT, &task->io); } @@ -205,7 +194,7 @@ static mooa_task_t *mooa_task_new(lua_State *L) { void mooa_task_loop(lua_State *L) { mooa_task_state_t *state = mooa_task_get_state(L); - struct ev_loop *loop = mooa_task_get_loop(L); + struct ev_loop *loop = EV_DEFAULT; mooa_task_t *task, *last; bool running; @@ -260,7 +249,7 @@ static int mooa_task_sleep(lua_State *L) { task = mooa_task_get(L); warnx("Sleeping task %p for %g seconds", task, secs); ev_timer_set(&task->timer, secs, 0.0); - ev_timer_start(mooa_task_get_loop(L), &task->timer); + ev_timer_start(EV_DEFAULT, &task->timer); return lua_yield(L, 0); } diff --git a/task.h b/task.h @@ -3,10 +3,7 @@ #include <lua.h> -struct ev_loop; - extern void mooa_task_init_state(lua_State *L); -extern struct ev_loop *mooa_task_get_loop(lua_State *L); extern void mooa_task_loop(lua_State *L); extern int mooa_task_open(lua_State *L); extern int mooa_task_spawn(lua_State *L); diff --git a/utils.c b/utils.c @@ -5,6 +5,8 @@ #include <lualib.h> #include <lauxlib.h> +#include "utils.h" + static void mooa_indent(int indent) { int i; @@ -14,11 +16,11 @@ static void mooa_indent(int indent) { } -void mooa_print_string(lua_State *L, int index) { +static void mooa_print_string(lua_State *L, int index) { char buf[1024]; const char *str; char c; - int i, j; + unsigned int i, j; size_t len; str = lua_tolstring(L, index, &len); @@ -29,7 +31,7 @@ void mooa_print_string(lua_State *L, int index) { buf[j] = c; j++; } else { - j += snprintf(&buf[j], sizeof(buf) - j, "\\x%02x", c); + j += (unsigned int)snprintf(&buf[j], sizeof(buf) - j, "\\x%02x", c); } } @@ -38,7 +40,7 @@ void mooa_print_string(lua_State *L, int index) { } -void mooa_print_pointer(lua_State *L, int index) { +static void mooa_print_pointer(lua_State *L, int index) { fprintf(stderr, "%s(%p)", luaL_typename(L, index), lua_topointer(L, index)); } @@ -55,10 +57,7 @@ void mooa_dump_stack(lua_State *L) { } -void mooa_print_value(lua_State *, int, int); - - -void mooa_print_table(lua_State *L, int index, int indent) { +static void mooa_print_table(lua_State *L, int index, int indent) { index = lua_absindex(L, index); lua_pushnil(L); fprintf(stderr, "{\n"); @@ -110,7 +109,7 @@ void mooa_print_value(lua_State *L, int index, int indent) { } -int mooa_utils_dump(lua_State *L) { +static int mooa_utils_dump(lua_State *L) { mooa_print_value(L, 1, 0); fprintf(stderr, "\n"); return 0;