1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
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 static void setEventBase(lua_State
* L
, struct event_base
* base
) {
14 struct event_base
** pbase
= lua_newuserdata(L
, sizeof(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);
28 static void freeCallbackArgs(le_callback
* arg
) {
30 lua_State
* L
= arg
->L
;
33 luaL_unref(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
34 luaL_unref(L
, LUA_REGISTRYINDEX
, arg
->objectRef
);
38 static int call_callback_function(lua_State
* L
, int argCount
) {
40 if(lua_pcall(L
, argCount
, 1, 0) || !(lua_isnil(L
, -1) || lua_isnumber(L
, -1))) {
41 printf("ERROR IN INIT: %s\n", lua_tostring(L
, -1));
45 /* Lua_isnil returns 1 if the value is nil... */
46 ret
= lua_tointeger(L
, -1) | -lua_isnil(L
, -1);
48 if(ret
< 0) { /* Done, no need to setup event */
51 if(ret
!= EV_READ
&& ret
!= EV_WRITE
) {
52 printf("BAD RET_VAL IN INIT: %i\n", ret
);
57 static void luaevent_callback(int fd
, short event
, void* p
);
59 static void setup_event(le_callback
* arg
, int fd
, short event
, int resetEvent
) {
61 if(resetEvent
) event_del(&arg
->ev
);
62 event_set(&arg
->ev
, fd
, event
| EV_PERSIST
, luaevent_callback
, arg
);
63 if(!resetEvent
) event_base_set(getEventBase(arg
->L
), &arg
->ev
);
64 event_add(&arg
->ev
, NULL
);
67 /* le_callback is allocated at the beginning of the coroutine in which it
68 is used, no need to manually de-allocate */
70 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
71 static void luaevent_callback(int fd
, short event
, void* p
) {
73 lua_State
* L
= arg
->L
;
75 lua_rawgeti(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
76 lua_pushinteger(L
, event
);
78 if(-1 == (ret
= call_callback_function(L
, 1))) {
79 freeCallbackArgs(arg
);
84 setup_event(arg
, fd
, ret
, 1);
87 static int luaevent_base_gc(lua_State
* L
) {
88 struct event_base
** pbase
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
90 event_base_free(*pbase
);
96 static int luaevent_cb_gc(lua_State
* L
) {
97 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
98 freeCallbackArgs(arg
);
102 static int luaevent_cb_getfd(lua_State
* L
) {
103 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
104 lua_pushinteger(L
, arg
->ev
.ev_fd
);
108 static int getSocketFd(lua_State
* L
, int idx
) {
110 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
111 lua_getfield(L
, idx
, "getfd");
113 return luaL_error(L
, "Socket type missing 'getfd' method");
114 lua_pushvalue(L
, idx
);
116 fd
= lua_tointeger(L
, -1);
121 static void push_new_callback(lua_State
* L
, int callbackRef
, int fd
, short event
) {
122 le_callback
* arg
= lua_newuserdata(L
, sizeof(*arg
));
123 luaL_getmetatable(L
, EVENT_CALLBACK_ARG_MT
);
124 lua_setmetatable(L
, -2);
127 arg
->callbackRef
= callbackRef
;
128 lua_pushvalue(L
, -1);
129 arg
->objectRef
= luaL_ref(L
, LUA_REGISTRYINDEX
);
130 setup_event(arg
, fd
, event
, 0);
132 /* Expected to be called at the beginning of the coro that uses it..
133 Value must be kept until coro is complete....
136 static int luaevent_addevent(lua_State
* L
) {
139 fd
= getSocketFd(L
, 1);
140 luaL_checktype(L
, 2, LUA_TFUNCTION
);
142 /* Preserve the callback function */
144 callbackRef
= luaL_ref(L
, LUA_REGISTRYINDEX
);
145 /* Call the callback with all arguments after it to get the loop primed.. */
146 if(-1 == (ret
= call_callback_function(L
, top
- 2))) {
147 luaL_unref(L
, LUA_REGISTRYINDEX
, callbackRef
);
151 push_new_callback(L
, callbackRef
, fd
, ret
);
155 static int luaevent_loop(lua_State
* L
) {
156 int ret
= event_base_loop(getEventBase(L
), 0);
157 lua_pushinteger(L
, ret
);
161 static luaL_Reg funcs
[] = {
162 { "addevent", luaevent_addevent
},
163 { "loop", luaevent_loop
},
172 static namedInteger consts
[] = {
174 {"EV_READ", EV_READ
},
175 {"EV_WRITE", EV_WRITE
},
179 void setNamedIntegers(lua_State
* L
, namedInteger
* p
) {
181 lua_pushinteger(L
, p
->value
);
182 lua_setfield(L
, -2, p
->name
);
188 int luaopen_luaevent_core(lua_State
* L
) {
189 /* Setup environ table */
190 lua_createtable(L
, 1, 0);
191 lua_replace(L
, LUA_ENVIRONINDEX
);
192 /* Setup metatable */
193 luaL_newmetatable(L
, EVENT_BASE_MT
);
194 lua_pushcfunction(L
, luaevent_base_gc
);
195 lua_setfield(L
, -2, "__gc");
197 luaL_newmetatable(L
, EVENT_CALLBACK_ARG_MT
);
198 lua_pushcfunction(L
, luaevent_cb_gc
);
199 lua_setfield(L
, -2, "__gc");
200 lua_pushcfunction(L
, luaevent_cb_getfd
);
201 lua_setfield(L
, -2, "getfd");
204 setEventBase(L
, event_init());
206 luaL_register(L
, "luaevent.core", funcs
);
207 setNamedIntegers(L
, consts
);