59 void *ret = lua_newuserdata(L, size);
62 lua_createtable(L, 0, 2);
64 lua_setfield(L, -2,
"__index");
65 lua_pushcfunction(L, gc);
66 lua_setfield(L, -2,
"__gc");
68 lua_setmetatable(L, -3);
70 luaI_openlib(L, NULL, reg, 1);
80 lua_getglobal(
state,
"__uloop_cb");
81 lua_rawgeti(
state, -1, tout->
r);
82 lua_remove(
state, -2);
84 lua_call(
state, 0, 0);
93 if (!lua_isnumber(L, -1)) {
94 lua_pushstring(L,
"invalid arg list");
100 set = lua_tointeger(L, -1);
101 tout = lua_touserdata(L, 1);
111 tout = lua_touserdata(L, 1);
123 lua_getfield(L, -1,
"__index");
124 lua_pushstring(L,
"__gc");
128 lua_getglobal(
state,
"__uloop_cb");
129 luaL_unref(
state, -1, tout->
r);
147 if (lua_isnumber(L, -1)) {
148 set = lua_tointeger(L, -1);
152 if (!lua_isfunction(L, -1)) {
153 lua_pushstring(L,
"invalid arg list");
159 lua_getglobal(L,
"__uloop_cb");
160 lua_pushvalue(L, -2);
161 ref = luaL_ref(L, -2);
177 lua_getglobal(
state,
"__uloop_cb");
178 lua_rawgeti(
state, -1, ufd->
r);
179 lua_remove(
state, -2);
182 lua_getglobal(
state,
"__uloop_fds");
184 lua_remove(
state, -2);
188 lua_call(
state, 2, 0);
194 if(lua_isnumber(L, idx)) {
195 fd = lua_tonumber(L, idx);
197 luaL_checktype(L, idx, LUA_TUSERDATA);
198 lua_getfield(L, idx,
"getfd");
200 return luaL_error(L,
"socket type missing 'getfd' method");
202 lua_pushvalue(L, idx > 0 ? idx: idx - 1);
204 fd = lua_tointeger(L, -1);
217 lua_getfield(L, -1,
"__index");
218 lua_pushstring(L,
"__gc");
222 lua_getglobal(
state,
"__uloop_cb");
223 luaL_unref(
state, -1, ufd->
r);
224 lua_remove(
state, -1);
226 lua_getglobal(
state,
"__uloop_fds");
228 lua_remove(
state, -1);
242 unsigned int flags = 0;
246 flags = luaL_checkinteger(L, 3);
248 lua_pushstring(L,
"flags cannot be zero");
252 luaL_checktype(L, 2, LUA_TFUNCTION);
256 lua_getglobal(L,
"__uloop_cb");
258 ref = luaL_ref(L, -2);
261 lua_getglobal(L,
"__uloop_fds");
263 fd_ref = luaL_ref(L, -2);
281 lua_getfield(L, -1,
"__index");
282 lua_pushstring(L,
"__gc");
286 if (proc->
r != LUA_NOREF) {
289 lua_getglobal(
state,
"__uloop_cb");
290 luaL_unref(
state, -1, proc->
r);
291 lua_remove(
state, -1);
302 lua_pushnumber(L, proc->
p.
pid);
319 lua_getglobal(
state,
"__uloop_cb");
320 lua_rawgeti(
state, -1, proc->
r);
322 luaL_unref(
state, -2, proc->
r);
324 lua_remove(
state, -2);
325 lua_pushinteger(
state, ret >> 8);
326 lua_call(
state, 1, 0);
335 if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
336 !lua_istable(L, -3) || !lua_isstring(L, -4)) {
337 lua_pushstring(L,
"invalid arg list");
346 lua_pushstring(L,
"failed to fork");
354 int argn = lua_objlen(L, -3);
355 int envn = lua_objlen(L, -2);
356 char** argp = malloc(
sizeof(
char*) * (argn + 2));
357 char** envp = malloc(
sizeof(
char*) * (envn + 1));
363 argp[0] = (
char*) lua_tostring(L, -4);
364 for (i = 1; i <= argn; i++) {
365 lua_rawgeti(L, -3, i);
366 argp[i] = (
char*) lua_tostring(L, -1);
371 for (i = 1; i <= envn; i++) {
372 lua_rawgeti(L, -2, i);
373 envp[i - 1] = (
char*) lua_tostring(L, -1);
378 execve(*argp, argp, envp);
382 lua_getglobal(L,
"__uloop_cb");
383 lua_pushvalue(L, -2);
384 ref = luaL_ref(L, -2);
399 lua_getglobal(
state,
"__uloop_cb");
400 lua_rawgeti(
state, -1, intv->
r);
401 lua_remove(
state, -2);
403 lua_call(
state, 0, 0);
411 if (!lua_isnumber(L, -1)) {
412 lua_pushstring(L,
"invalid arg list");
418 set = lua_tointeger(L, -1);
419 intv = lua_touserdata(L, 1);
429 intv = lua_touserdata(L, 1);
438 intv = lua_touserdata(L, 1);
450 lua_getfield(L, -1,
"__index");
451 lua_pushstring(L,
"__gc");
455 lua_getglobal(
state,
"__uloop_cb");
456 luaL_unref(
state, -1, intv->
r);
472 unsigned int set = 0;
475 if (lua_isnumber(L, -1)) {
476 set = lua_tointeger(L, -1);
480 if (!lua_isfunction(L, -1)) {
481 lua_pushstring(L,
"invalid arg list");
487 lua_getglobal(L,
"__uloop_cb");
488 lua_pushvalue(L, -2);
489 ref = luaL_ref(L, -2);
505 lua_getglobal(
state,
"__uloop_cb");
506 lua_rawgeti(
state, -1, sig->
r);
507 lua_remove(
state, -2);
510 lua_call(
state, 1, 0);
517 lua_pushinteger(L, sig->
s.
signo);
529 lua_getfield(L, -1,
"__index");
530 lua_pushstring(L,
"__gc");
534 lua_getglobal(
state,
"__uloop_cb");
535 luaL_unref(
state, -1, sig->
r);
552 if (lua_isnumber(L, -1)) {
553 signo = lua_tointeger(L, -1);
557 if (!lua_isfunction(L, -1) || signo <= 0 || signo > NSIG) {
558 lua_pushstring(L,
"invalid arg list");
564 lua_getglobal(L,
"__uloop_cb");
565 lua_pushvalue(L, -2);
566 ref = luaL_ref(L, -2);
581 lua_pushboolean(L, 1);
589 lua_pushboolean(L, 1);
620 lua_createtable(L, 1, 0);
621 lua_setglobal(L,
"__uloop_cb");
623 lua_createtable(L, 1, 0);
624 lua_setglobal(L,
"__uloop_fds");
627 lua_pushstring(L,
"_VERSION");
628 lua_pushstring(L,
"1.0");
631 lua_pushstring(L,
"ULOOP_READ");
635 lua_pushstring(L,
"ULOOP_WRITE");
639 lua_pushstring(L,
"ULOOP_EDGE_TRIGGER");
643 lua_pushstring(L,
"ULOOP_BLOCKING");
647 #define SIGNAME_CONST(signame) do { \
648 lua_pushstring(L, #signame); \
649 lua_pushinteger(L, signame); \
689 #if defined(SIGSTKFLT)
731 #if defined(SIGVTALRM)
749 lua_pushstring(L,
"Called");
#define container_of(ptr, type, member)
static const luaL_Reg timer_m[]
static int ul_ufd_delete(lua_State *L)
static luaL_reg uloop_func[]
static const luaL_Reg ufd_m[]
static const luaL_Reg process_m[]
static int ul_run(lua_State *L)
static int ul_interval_free(lua_State *L)
static void ul_signal_cb(struct uloop_signal *s)
static void ul_process_cb(struct uloop_process *p, int ret)
static int ul_signal_signo(lua_State *L)
static int ul_process_pid(lua_State *L)
static int ul_process(lua_State *L)
static int ul_end(lua_State *L)
static int ul_signal(lua_State *L)
static int ul_init(lua_State *L)
static int ul_timer_free(lua_State *L)
static int ul_process_free(lua_State *L)
#define SIGNAME_CONST(signame)
static int ul_signal_free(lua_State *L)
static int ul_interval_set(lua_State *L)
static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
static const luaL_Reg interval_m[]
int luaopen_uloop(lua_State *L)
static int ul_timer_set(lua_State *L)
static int get_sock_fd(lua_State *L, int idx)
int luaclose_uloop(lua_State *L)
static const luaL_Reg signal_m[]
static void * ul_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
static int ul_timer_remaining(lua_State *L)
static int ul_interval_expirations(lua_State *L)
static void ul_interval_cb(struct uloop_interval *i)
static void ul_timer_cb(struct uloop_timeout *t)
static int ul_interval(lua_State *L)
static int ul_ufd_add(lua_State *L)
static int ul_timer(lua_State *L)
static int ul_interval_remaining(lua_State *L)
uloop_interval_handler cb
static struct epoll_event events[ULOOP_MAX_EVENTS]
int64_t uloop_timeout_remaining64(struct uloop_timeout *timeout)
int uloop_signal_add(struct uloop_signal *s)
int uloop_interval_set(struct uloop_interval *timer, unsigned int msecs)
int uloop_process_delete(struct uloop_process *p)
int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
int uloop_timeout_cancel(struct uloop_timeout *timeout)
int uloop_fd_delete(struct uloop_fd *fd)
int uloop_signal_delete(struct uloop_signal *s)
int64_t uloop_interval_remaining(struct uloop_interval *timer)
int uloop_interval_cancel(struct uloop_interval *timer)
int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
int uloop_process_add(struct uloop_process *p)
static void uloop_end(void)
static int uloop_run(void)
#define ULOOP_EDGE_TRIGGER