luaevent/src/luaevent.c

changeset 2
01b3a96ae760
parent 1
31c782cfe7fe
child 3
5999243fab1d
equal deleted inserted replaced
1:31c782cfe7fe 2:01b3a96ae760
72 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT); 72 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
73 freeCallbackArgs(arg); 73 freeCallbackArgs(arg);
74 return 0; 74 return 0;
75 } 75 }
76 76
77 static int luaevent_cb_getfd(lua_State* L) {
78 le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
79 lua_pushinteger(L, arg->ev.ev_fd);
80 return 1;
81 }
82
77 int getSocketFd(lua_State* L, int idx) { 83 int getSocketFd(lua_State* L, int idx) {
78 int fd; 84 int fd;
79 luaL_checktype(L, idx, LUA_TUSERDATA); 85 luaL_checktype(L, idx, LUA_TUSERDATA);
80 lua_getfield(L, idx, "getfd"); 86 lua_getfield(L, idx, "getfd");
81 if(lua_isnil(L, -1)) 87 if(lua_isnil(L, -1))
88 } 94 }
89 95
90 /* Expected to be called at the beginning of the coro that uses it.. 96 /* Expected to be called at the beginning of the coro that uses it..
91 Value must be kept until coro is complete.... 97 Value must be kept until coro is complete....
92 */ 98 */
93 /* sock, event, callback */ 99 /* sock, callback */
94 static int luaevent_addevent(lua_State* L) { 100 static int luaevent_addevent(lua_State* L) {
95 int fd, event, callbackRef; 101 int fd, callbackRef;
102 int top, ret;
96 le_callback* arg; 103 le_callback* arg;
97 fd = getSocketFd(L, 1); 104 fd = getSocketFd(L, 1);
98 event = luaL_checkinteger(L, 2); 105 luaL_checktype(L, 2, LUA_TFUNCTION);
99 luaL_checktype(L, 3, LUA_TFUNCTION); 106 top = lua_gettop(L);
100 lua_pushvalue(L, 3); 107 /* Preserve the callback function */
108 lua_pushvalue(L, 2);
101 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); 109 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
110
111 /* Call the callback with all arguments after it to get the loop primed.. */
112 lua_call(L, top - 2, 1);
113 ret = lua_tointeger(L, -1);
114 lua_pop(L, 1);
115 if(ret == -1) { /* Done, no need to setup event */
116 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
117 return 0;
118 }
102 arg = lua_newuserdata(L, sizeof(*arg)); 119 arg = lua_newuserdata(L, sizeof(*arg));
103 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT); 120 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
104 lua_setmetatable(L, -2); 121 lua_setmetatable(L, -2);
105 122
106 arg->L = L; 123 arg->L = L;
107 arg->callbackRef = callbackRef; 124 arg->callbackRef = callbackRef;
125
108 /* Setup event... */ 126 /* Setup event... */
109 event_set(&arg->ev, fd, event | EV_PERSIST, luaevent_callback, arg); 127 event_set(&arg->ev, fd, ret | EV_PERSIST, luaevent_callback, arg);
110 event_base_set(getEventBase(L), &arg->ev); 128 event_base_set(getEventBase(L), &arg->ev);
111 event_add(&arg->ev, NULL); 129 event_add(&arg->ev, NULL);
112 return 1; 130 return 1;
113 } 131 }
114 132
155 lua_setfield(L, -2, "__gc"); 173 lua_setfield(L, -2, "__gc");
156 lua_pop(L, 1); 174 lua_pop(L, 1);
157 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT); 175 luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
158 lua_pushcfunction(L, luaevent_cb_gc); 176 lua_pushcfunction(L, luaevent_cb_gc);
159 lua_setfield(L, -2, "__gc"); 177 lua_setfield(L, -2, "__gc");
178 lua_pushcfunction(L, luaevent_cb_getfd);
179 lua_setfield(L, -2, "getfd");
160 lua_pop(L, 1); 180 lua_pop(L, 1);
161 181
162 setEventBase(L, event_init()); 182 setEventBase(L, event_init());
163 183
164 luaL_register(L, "luaevent.core", funcs); 184 luaL_register(L, "luaevent.core", funcs);

mercurial