Fixed stack overflow issue. (Forgot to pop error/integer)
[luaevent.git] / luaevent / src / luaevent.c
blob286d95eeb11fab003199525bbaccc6fe1172a83a
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>
9 #define EVENT_BASE_MT "EVENT_BASE_MT"
10 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
11 #define EVENT_BASE_LOCATION 1
13 void setEventBase(lua_State* L, struct event_base* base) {
14 struct event_base** pbase = lua_newuserdata(L, sizeof(base));
15 *pbase = base;
16 luaL_getmetatable(L, EVENT_BASE_MT);
17 lua_setmetatable(L, -2);
18 lua_rawseti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
20 struct event_base* getEventBase(lua_State* L) {
21 struct event_base* base;
22 lua_rawgeti(L, LUA_ENVIRONINDEX, EVENT_BASE_LOCATION);
23 base = *(struct event_base**)lua_topointer(L, -1);
24 lua_pop(L, 1);
25 return base;
28 void freeCallbackArgs(le_callback* arg) {
29 if(arg->L) {
30 lua_State* L = arg->L;
31 arg->L = NULL;
32 event_del(&arg->ev);
33 luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
36 /* le_callback is allocated at the beginning of the coroutine in which it
37 is used, no need to manually de-allocate */
39 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
40 static void luaevent_callback(int fd, short event, void* p) {
41 le_callback* arg = p;
42 lua_State* L = arg->L;
43 int ret;
44 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
45 lua_pushinteger(L, event);
46 if(lua_pcall(L, 1, 1, 0) || !lua_isnumber(L, -1)) {
47 printf("ERROR IN CB: %s\n", lua_tostring(L, -1));
48 lua_pop(L, 1);
49 freeCallbackArgs(arg);
50 return;
52 ret = lua_tointeger(L, -1);
53 lua_pop(L, 1);
54 if(ret == -1) {
55 freeCallbackArgs(arg);
56 return;
58 if(ret != EV_READ && ret != EV_WRITE) {
59 printf("BAD RET_VAL: %i\n", ret);
62 struct event *ev = &arg->ev;
63 int newEvent = ret;
64 if(newEvent != event) { // Need to hook up new event...
65 event_del(ev);
66 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
67 event_add(ev, NULL);
71 static int luaevent_base_gc(lua_State* L) {
72 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
73 if(*pbase) {
74 event_base_free(*pbase);
75 *pbase = NULL;
77 return 0;
80 static int luaevent_cb_gc(lua_State* L) {
81 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
82 freeCallbackArgs(arg);
83 return 0;
86 static int luaevent_cb_getfd(lua_State* L) {
87 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
88 lua_pushinteger(L, arg->ev.ev_fd);
89 return 1;
92 int getSocketFd(lua_State* L, int idx) {
93 int fd;
94 luaL_checktype(L, idx, LUA_TUSERDATA);
95 lua_getfield(L, idx, "getfd");
96 if(lua_isnil(L, -1))
97 return luaL_error(L, "Socket type missing 'getfd' method");
98 lua_pushvalue(L, idx);
99 lua_call(L, 1, 1);
100 fd = lua_tointeger(L, -1);
101 lua_pop(L, 1);
102 return fd;
105 /* Expected to be called at the beginning of the coro that uses it..
106 Value must be kept until coro is complete....
108 /* sock, callback */
109 static int luaevent_addevent(lua_State* L) {
110 int fd, callbackRef;
111 int top, ret;
112 le_callback* arg;
113 fd = getSocketFd(L, 1);
114 luaL_checktype(L, 2, LUA_TFUNCTION);
115 top = lua_gettop(L);
116 /* Preserve the callback function */
117 lua_pushvalue(L, 2);
118 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
120 /* Call the callback with all arguments after it to get the loop primed.. */
121 if(lua_pcall(L, top - 2, 1, 0) || !lua_isnumber(L, -1)) {
122 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1));
123 lua_pop(L, 1);
124 return 0;
126 ret = lua_tointeger(L, -1);
127 lua_pop(L, 1);
128 if(ret == -1) { /* Done, no need to setup event */
129 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
130 return 0;
132 if(ret != EV_READ && ret != EV_WRITE) {
133 printf("BAD RET_VAL IN INIT: %i\n", ret);
135 arg = lua_newuserdata(L, sizeof(*arg));
136 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
137 lua_setmetatable(L, -2);
139 arg->L = L;
140 arg->callbackRef = callbackRef;
142 /* Setup event... */
143 event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg);
144 event_base_set(getEventBase(L), &arg->ev);
145 event_add(&arg->ev, NULL);
146 return 1;
149 static int luaevent_loop(lua_State* L) {
150 int ret = event_base_loop(getEventBase(L), 0);
151 lua_pushinteger(L, ret);
152 return 1;
155 static luaL_Reg funcs[] = {
156 { "addevent", luaevent_addevent },
157 { "loop", luaevent_loop },
158 { NULL, NULL }
161 typedef struct {
162 const char* name;
163 int value;
164 } namedInteger;
166 static namedInteger consts[] = {
167 {"LEAVE", -1},
168 {"EV_READ", EV_READ},
169 {"EV_WRITE", EV_WRITE},
170 {NULL, 0}
173 void setNamedIntegers(lua_State* L, namedInteger* p) {
174 while(p->name) {
175 lua_pushinteger(L, p->value);
176 lua_setfield(L, -2, p->name);
177 p++;
181 /* Verified ok */
182 int luaopen_luaevent_core(lua_State* L) {
183 /* Setup environ table */
184 lua_createtable(L, 1, 0);
185 lua_replace(L, LUA_ENVIRONINDEX);
186 /* Setup metatable */
187 luaL_newmetatable(L, EVENT_BASE_MT);
188 lua_pushcfunction(L, luaevent_base_gc);
189 lua_setfield(L, -2, "__gc");
190 lua_pop(L, 1);
191 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
192 lua_pushcfunction(L, luaevent_cb_gc);
193 lua_setfield(L, -2, "__gc");
194 lua_pushcfunction(L, luaevent_cb_getfd);
195 lua_setfield(L, -2, "getfd");
196 lua_pop(L, 1);
198 setEventBase(L, event_init());
200 luaL_register(L, "luaevent.core", funcs);
201 setNamedIntegers(L, consts);
202 return 1;