Unlearned get/setMainThread functions since they were unused.
[luaevent.git] / src / luaevent.c
blobc9ce3d1fb8415f6fa95fa4435276e05b00906568
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
4 #include "luaevent.h"
6 #include <lua.h>
7 #include <lauxlib.h>
8 #include <assert.h>
10 #define EVENT_BASE_MT "EVENT_BASE_MT"
11 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
13 int luaevent_newbase(lua_State* L) {
14 le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
15 base->loop_L = NULL; /* No running loop */
16 base->base = event_init();
17 luaL_getmetatable(L, EVENT_BASE_MT);
18 lua_setmetatable(L, -2);
19 return 1;
22 void freeCallbackArgs(le_callback* arg, lua_State* L) {
23 if(arg->base) {
24 arg->base = NULL;
25 event_del(&arg->ev);
26 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
29 /* le_callback is allocated at the beginning of the coroutine in which it
30 is used, no need to manually de-allocate */
32 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
33 static void luaevent_callback(int fd, short event, void* p) {
34 le_callback* arg = p;
35 lua_State* L;
36 int ret;
37 assert(arg && arg->base && arg->base->loop_L);
38 L = arg->base->loop_L;
39 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
40 lua_pushinteger(L, event);
41 lua_call(L, 1, 1);
42 ret = lua_tointeger(L, -1);
43 lua_pop(L, 1);
44 if(ret == -1) {
45 freeCallbackArgs(arg, L);
46 } else {
47 struct event *ev = &arg->ev;
48 int newEvent = ret;
49 if(newEvent != event) { // Need to hook up new event...
50 event_del(ev);
51 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
52 event_add(ev, NULL);
57 static int luaevent_base_gc(lua_State* L) {
58 le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
59 if(base->base) {
60 event_base_free(base->base);
61 base->base = NULL;
63 return 0;
66 static int luaevent_cb_gc(lua_State* L) {
67 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
68 freeCallbackArgs(arg, L);
69 return 0;
72 int getSocketFd(lua_State* L, int idx) {
73 int fd;
74 luaL_checktype(L, idx, LUA_TUSERDATA);
75 lua_getfield(L, idx, "getfd");
76 if(lua_isnil(L, -1))
77 return luaL_error(L, "Socket type missing 'getfd' method");
78 lua_pushvalue(L, idx);
79 lua_call(L, 1, 1);
80 fd = lua_tointeger(L, -1);
81 lua_pop(L, 1);
82 return fd;
85 /* sock, event, callback */
86 static int luaevent_addevent(lua_State* L) {
87 int fd, event, callbackRef;
88 le_callback* arg;
89 le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
90 fd = getSocketFd(L, 2);
91 event = luaL_checkinteger(L, 3);
92 luaL_checktype(L, 4, LUA_TFUNCTION);
93 lua_pushvalue(L, 4);
94 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
95 arg = lua_newuserdata(L, sizeof(*arg));
96 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
97 lua_setmetatable(L, -2);
99 arg->base = base;
100 arg->callbackRef = callbackRef;
101 /* Setup event... */
102 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg);
103 event_base_set(base->base, &arg->ev);
104 event_add(&arg->ev, NULL);
105 return 1;
108 static int luaevent_loop(lua_State* L) {
109 le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
110 base->loop_L = L;
111 int ret = event_base_loop(base->base, 0);
112 lua_pushinteger(L, ret);
113 return 1;
116 static luaL_Reg base_funcs[] = {
117 { "addevent", luaevent_addevent },
118 { "loop", luaevent_loop },
119 { NULL, NULL }
122 static luaL_Reg funcs[] = {
123 { "new", luaevent_newbase },
124 { NULL, NULL }
127 typedef struct {
128 const char* name;
129 int value;
130 } namedInteger;
132 static namedInteger consts[] = {
133 {"LEAVE", -1},
134 {"EV_READ", EV_READ},
135 {"EV_WRITE", EV_WRITE},
136 {NULL, 0}
139 void setNamedIntegers(lua_State* L, namedInteger* p) {
140 while(p->name) {
141 lua_pushinteger(L, p->value);
142 lua_setfield(L, -2, p->name);
143 p++;
147 /* Verified ok */
148 int luaopen_luaevent_core(lua_State* L) {
149 /* Setup metatable */
150 luaL_newmetatable(L, EVENT_BASE_MT);
151 lua_newtable(L);
152 luaL_register(L, NULL, base_funcs);
153 lua_setfield(L, -2, "__index");
154 lua_pushcfunction(L, luaevent_base_gc);
155 lua_setfield(L, -2, "__gc");
156 lua_pop(L, 1);
157 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
158 lua_pushcfunction(L, luaevent_cb_gc);
159 lua_setfield(L, -2, "__gc");
160 lua_newtable(L);
161 lua_pushcfunction(L, luaevent_cb_gc);
162 lua_setfield(L, -2, "close");
163 lua_setfield(L, -2, "__index");
164 lua_pop(L, 1);
166 luaL_register(L, "luaevent.core", funcs);
167 setNamedIntegers(L, consts);
168 return 1;