1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
2 * Licensed as LGPL - See doc/COPYING for details */
3 #include "event_callback.h"
7 void freeCallbackArgs(le_callback
* arg
, lua_State
* L
) {
11 luaL_unref(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
14 /* le_callback is allocated at the beginning of the coroutine in which it
15 is used, no need to manually de-allocate */
17 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
18 void luaevent_callback(int fd
, short event
, void* p
) {
22 assert(arg
&& arg
->base
&& arg
->base
->loop_L
);
23 L
= arg
->base
->loop_L
;
24 lua_rawgeti(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
25 lua_pushinteger(L
, event
);
27 ret
= lua_tointeger(L
, -1);
30 freeCallbackArgs(arg
, L
);
32 struct event
*ev
= &arg
->ev
;
34 if(newEvent
!= event
) { // Need to hook up new event...
36 event_set(ev
, fd
, EV_PERSIST
| newEvent
, luaevent_callback
, arg
);
42 static int luaevent_cb_gc(lua_State
* L
) {
43 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
44 freeCallbackArgs(arg
, L
);
48 int event_callback_register(lua_State
* L
) {
49 luaL_newmetatable(L
, EVENT_CALLBACK_ARG_MT
);
50 lua_pushcfunction(L
, luaevent_cb_gc
);
51 lua_setfield(L
, -2, "__gc");
53 lua_pushcfunction(L
, luaevent_cb_gc
);
54 lua_setfield(L
, -2, "close");
55 lua_setfield(L
, -2, "__index");