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 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
9 void freeCallbackArgs(le_callback
* arg
, lua_State
* L
) {
13 luaL_unref(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
16 /* le_callback is allocated at the beginning of the coroutine in which it
17 is used, no need to manually de-allocate */
19 /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
20 void luaevent_callback(int fd
, short event
, void* p
) {
24 assert(arg
&& arg
->base
&& arg
->base
->loop_L
);
25 L
= arg
->base
->loop_L
;
26 lua_rawgeti(L
, LUA_REGISTRYINDEX
, arg
->callbackRef
);
27 lua_pushinteger(L
, event
);
29 ret
= lua_tointeger(L
, -1);
32 freeCallbackArgs(arg
, L
);
34 struct event
*ev
= &arg
->ev
;
36 if(newEvent
!= event
) { // Need to hook up new event...
38 event_set(ev
, fd
, EV_PERSIST
| newEvent
, luaevent_callback
, arg
);
44 static int luaevent_cb_gc(lua_State
* L
) {
45 le_callback
* arg
= luaL_checkudata(L
, 1, EVENT_CALLBACK_ARG_MT
);
46 freeCallbackArgs(arg
, L
);
50 le_callback
* event_callback_push(lua_State
* L
, int baseIdx
, int callbackIdx
) {
52 le_base
*base
= event_base_get(L
, baseIdx
);
53 luaL_checktype(L
, callbackIdx
, LUA_TFUNCTION
);
54 cb
= lua_newuserdata(L
, sizeof(*cb
));
55 luaL_getmetatable(L
, EVENT_CALLBACK_ARG_MT
);
56 lua_setmetatable(L
, -2);
58 lua_pushvalue(L
, callbackIdx
);
59 cb
->callbackRef
= luaL_ref(L
, LUA_REGISTRYINDEX
);
64 int event_callback_register(lua_State
* L
) {
65 luaL_newmetatable(L
, EVENT_CALLBACK_ARG_MT
);
66 lua_pushcfunction(L
, luaevent_cb_gc
);
67 lua_setfield(L
, -2, "__gc");
69 lua_pushcfunction(L
, luaevent_cb_gc
);
70 lua_setfield(L
, -2, "close");
71 lua_setfield(L
, -2, "__index");