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 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 void freeCallbackArgs(le_callback
* arg
) {
30 lua_State
* L
= arg
->L
;
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
) {
42 lua_State
* L
= arg
->L
;
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));
49 freeCallbackArgs(arg
);
52 ret
= lua_tointeger(L
, -1);
55 freeCallbackArgs(arg
);
58 if(ret
!= EV_READ
&& ret
!= EV_WRITE
) {
59 printf("BAD RET_VAL: %i\n", ret
);
62 struct event
*ev
= &arg
->ev
;
64 if(newEvent
!= event
) { // Need to hook up new event...
66 event_set(ev
, fd
, EV_PERSIST
| newEvent
, luaevent_callback
, arg
);
71 static int luaevent_base_gc(lua_State
* L
) {
72 struct event_base
** pbase
= luaL_checkudata(L
, 1, EVENT_BASE_MT
);
74 event_base_free(*pbase
);
80 static int luaevent_cb_gc(lua_State
* L
) {
81 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
82 freeCallbackArgs(arg
);
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
);
92 int getSocketFd(lua_State
* L
, int idx
) {
94 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
95 lua_getfield(L
, idx
, "getfd");
97 return luaL_error(L
, "Socket type missing 'getfd' method");
98 lua_pushvalue(L
, idx
);
100 fd
= lua_tointeger(L
, -1);
105 /* Expected to be called at the beginning of the coro that uses it..
106 Value must be kept until coro is complete....
109 static int luaevent_addevent(lua_State
* L
) {
113 fd
= getSocketFd(L
, 1);
114 luaL_checktype(L
, 2, LUA_TFUNCTION
);
116 /* Preserve the callback function */
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));
126 ret
= lua_tointeger(L
, -1);
128 if(ret
== -1) { /* Done, no need to setup event */
129 luaL_unref(L
, LUA_REGISTRYINDEX
, callbackRef
);
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);
140 arg
->callbackRef
= callbackRef
;
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
);
149 static int luaevent_loop(lua_State
* L
) {
150 int ret
= event_base_loop(getEventBase(L
), 0);
151 lua_pushinteger(L
, ret
);
155 static luaL_Reg funcs
[] = {
156 { "addevent", luaevent_addevent
},
157 { "loop", luaevent_loop
},
166 static namedInteger consts
[] = {
168 {"EV_READ", EV_READ
},
169 {"EV_WRITE", EV_WRITE
},
173 void setNamedIntegers(lua_State
* L
, namedInteger
* p
) {
175 lua_pushinteger(L
, p
->value
);
176 lua_setfield(L
, -2, p
->name
);
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");
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");
198 setEventBase(L
, event_init());
200 luaL_register(L
, "luaevent.core", funcs
);
201 setNamedIntegers(L
, consts
);