1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
1 /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
2 * Licensed as LGPL - See doc/COPYING for details */ |
2 * Licensed as LGPL - See doc/COPYING for details */ |
3 #include "event_callback.h" |
3 #include "event_callback.h" |
4 #include <assert.h> |
4 #include <assert.h> |
5 #include <lauxlib.h> |
5 #include <lauxlib.h> |
|
6 |
|
7 #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
6 |
8 |
7 void freeCallbackArgs(le_callback* arg, lua_State* L) { |
9 void freeCallbackArgs(le_callback* arg, lua_State* L) { |
8 if(arg->base) { |
10 if(arg->base) { |
9 arg->base = NULL; |
11 arg->base = NULL; |
10 event_del(&arg->ev); |
12 event_del(&arg->ev); |
43 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT); |
45 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT); |
44 freeCallbackArgs(arg, L); |
46 freeCallbackArgs(arg, L); |
45 return 0; |
47 return 0; |
46 } |
48 } |
47 |
49 |
|
50 le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) { |
|
51 le_callback* cb; |
|
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); |
|
57 |
|
58 lua_pushvalue(L, callbackIdx); |
|
59 cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); |
|
60 cb->base = base; |
|
61 return cb; |
|
62 } |
|
63 |
48 int event_callback_register(lua_State* L) { |
64 int event_callback_register(lua_State* L) { |
49 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT); |
65 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT); |
50 lua_pushcfunction(L, luaevent_cb_gc); |
66 lua_pushcfunction(L, luaevent_cb_gc); |
51 lua_setfield(L, -2, "__gc"); |
67 lua_setfield(L, -2, "__gc"); |
52 lua_newtable(L); |
68 lua_newtable(L); |