Mon, 11 Jun 2007 01:52:16 +0000
Fixed stack overflow issue. (Forgot to pop error/integer)
1
31c782cfe7fe
* Adjusted licensing and added README.
Thomas Harning Jr <harningt@gmail.com>
parents:
0
diff
changeset
|
1 | /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
31c782cfe7fe
* Adjusted licensing and added README.
Thomas Harning Jr <harningt@gmail.com>
parents:
0
diff
changeset
|
2 | * Licensed as LGPL - See doc/COPYING for details */ |
31c782cfe7fe
* Adjusted licensing and added README.
Thomas Harning Jr <harningt@gmail.com>
parents:
0
diff
changeset
|
3 | |
0 | 4 | #include "luaevent.h" |
5 | ||
6 | #include <lua.h> | |
7 | #include <lauxlib.h> | |
8 | ||
9 | #define EVENT_BASE_MT "EVENT_BASE_MT" | |
10 | #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" | |
11 | #define EVENT_BASE_LOCATION 1 | |
12 | ||
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); | |
19 | } | |
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; | |
26 | } | |
27 | ||
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); | |
34 | } | |
35 | } | |
36 | /* le_callback is allocated at the beginning of the coroutine in which it | |
37 | is used, no need to manually de-allocate */ | |
38 | ||
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); | |
3
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
46 | if(lua_pcall(L, 1, 1, 0) || !lua_isnumber(L, -1)) { |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
47 | printf("ERROR IN CB: %s\n", lua_tostring(L, -1)); |
4
4d0e9388214a
Fixed stack overflow issue. (Forgot to pop error/integer)
Thomas Harning Jr <harningt@gmail.com>
parents:
3
diff
changeset
|
48 | lua_pop(L, 1); |
3
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
49 | freeCallbackArgs(arg); |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
50 | return; |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
51 | } |
0 | 52 | ret = lua_tointeger(L, -1); |
53 | lua_pop(L, 1); | |
54 | if(ret == -1) { | |
55 | freeCallbackArgs(arg); | |
3
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
56 | return; |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
57 | } |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
58 | if(ret != EV_READ && ret != EV_WRITE) { |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
59 | printf("BAD RET_VAL: %i\n", ret); |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
60 | } |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
61 | |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
62 | struct event *ev = &arg->ev; |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
63 | int newEvent = ret; |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
64 | if(newEvent != event) { // Need to hook up new event... |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
65 | event_del(ev); |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
66 | event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg); |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
67 | event_add(ev, NULL); |
0 | 68 | } |
69 | } | |
70 | ||
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; | |
76 | } | |
77 | return 0; | |
78 | } | |
79 | ||
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; | |
84 | } | |
85 | ||
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
86 | static int luaevent_cb_getfd(lua_State* L) { |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
87 | le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
88 | lua_pushinteger(L, arg->ev.ev_fd); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
89 | return 1; |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
90 | } |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
91 | |
0 | 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; | |
103 | } | |
104 | ||
105 | /* Expected to be called at the beginning of the coro that uses it.. | |
106 | Value must be kept until coro is complete.... | |
107 | */ | |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
108 | /* sock, callback */ |
0 | 109 | static int luaevent_addevent(lua_State* L) { |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
110 | int fd, callbackRef; |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
111 | int top, ret; |
0 | 112 | le_callback* arg; |
113 | fd = getSocketFd(L, 1); | |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
114 | luaL_checktype(L, 2, LUA_TFUNCTION); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
115 | top = lua_gettop(L); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
116 | /* Preserve the callback function */ |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
117 | lua_pushvalue(L, 2); |
0 | 118 | callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
119 | |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
120 | /* Call the callback with all arguments after it to get the loop primed.. */ |
3
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
121 | if(lua_pcall(L, top - 2, 1, 0) || !lua_isnumber(L, -1)) { |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
122 | printf("ERROR IN INIT: %s\n", lua_tostring(L, -1)); |
4
4d0e9388214a
Fixed stack overflow issue. (Forgot to pop error/integer)
Thomas Harning Jr <harningt@gmail.com>
parents:
3
diff
changeset
|
123 | lua_pop(L, 1); |
3
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
124 | return 0; |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
125 | } |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
126 | ret = lua_tointeger(L, -1); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
127 | lua_pop(L, 1); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
128 | if(ret == -1) { /* Done, no need to setup event */ |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
129 | luaL_unref(L, LUA_REGISTRYINDEX, callbackRef); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
130 | return 0; |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
131 | } |
3
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
132 | if(ret != EV_READ && ret != EV_WRITE) { |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
133 | printf("BAD RET_VAL IN INIT: %i\n", ret); |
5999243fab1d
* Added some cheap protection code for failures in callback
Thomas Harning Jr <harningt@gmail.com>
parents:
2
diff
changeset
|
134 | } |
0 | 135 | arg = lua_newuserdata(L, sizeof(*arg)); |
136 | luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT); | |
137 | lua_setmetatable(L, -2); | |
138 | ||
139 | arg->L = L; | |
140 | arg->callbackRef = callbackRef; | |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
141 | |
0 | 142 | /* Setup event... */ |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
143 | event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg); |
0 | 144 | event_base_set(getEventBase(L), &arg->ev); |
145 | event_add(&arg->ev, NULL); | |
146 | return 1; | |
147 | } | |
148 | ||
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; | |
153 | } | |
154 | ||
155 | static luaL_Reg funcs[] = { | |
156 | { "addevent", luaevent_addevent }, | |
157 | { "loop", luaevent_loop }, | |
158 | { NULL, NULL } | |
159 | }; | |
160 | ||
161 | typedef struct { | |
162 | const char* name; | |
163 | int value; | |
164 | } namedInteger; | |
165 | ||
166 | static namedInteger consts[] = { | |
167 | {"LEAVE", -1}, | |
168 | {"EV_READ", EV_READ}, | |
169 | {"EV_WRITE", EV_WRITE}, | |
170 | {NULL, 0} | |
171 | }; | |
172 | ||
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++; | |
178 | } | |
179 | } | |
180 | ||
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"); | |
2
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
194 | lua_pushcfunction(L, luaevent_cb_getfd); |
01b3a96ae760
* Completed mostly working version
Thomas Harning Jr <harningt@gmail.com>
parents:
1
diff
changeset
|
195 | lua_setfield(L, -2, "getfd"); |
0 | 196 | lua_pop(L, 1); |
197 | ||
198 | setEventBase(L, event_init()); | |
199 | ||
200 | luaL_register(L, "luaevent.core", funcs); | |
201 | setNamedIntegers(L, consts); | |
202 | return 1; | |
203 | } |