luaevent/src/luaevent.c

changeset 3
5999243fab1d
parent 2
01b3a96ae760
child 4
4d0e9388214a
equal deleted inserted replaced
2:01b3a96ae760 3:5999243fab1d
41 le_callback* arg = p; 41 le_callback* arg = p;
42 lua_State* L = arg->L; 42 lua_State* L = arg->L;
43 int ret; 43 int ret;
44 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef); 44 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
45 lua_pushinteger(L, event); 45 lua_pushinteger(L, event);
46 lua_call(L, 1, 1); 46 if(lua_pcall(L, 1, 1, 0) || !lua_isnumber(L, -1)) {
47 printf("ERROR IN CB: %s\n", lua_tostring(L, -1));
48 freeCallbackArgs(arg);
49 return;
50 }
47 ret = lua_tointeger(L, -1); 51 ret = lua_tointeger(L, -1);
48 lua_pop(L, 1); 52 lua_pop(L, 1);
49 if(ret == -1) { 53 if(ret == -1) {
50 freeCallbackArgs(arg); 54 freeCallbackArgs(arg);
51 } else { 55 return;
52 struct event *ev = &arg->ev; 56 }
53 int newEvent = ret; 57 if(ret != EV_READ && ret != EV_WRITE) {
54 if(newEvent != event) { // Need to hook up new event... 58 printf("BAD RET_VAL: %i\n", ret);
55 event_del(ev); 59 }
56 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg); 60
57 event_add(ev, NULL); 61 struct event *ev = &arg->ev;
58 } 62 int newEvent = ret;
63 if(newEvent != event) { // Need to hook up new event...
64 event_del(ev);
65 event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg);
66 event_add(ev, NULL);
59 } 67 }
60 } 68 }
61 69
62 static int luaevent_base_gc(lua_State* L) { 70 static int luaevent_base_gc(lua_State* L) {
63 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT); 71 struct event_base** pbase = luaL_checkudata(L, 1, EVENT_BASE_MT);
107 /* Preserve the callback function */ 115 /* Preserve the callback function */
108 lua_pushvalue(L, 2); 116 lua_pushvalue(L, 2);
109 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); 117 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
110 118
111 /* Call the callback with all arguments after it to get the loop primed.. */ 119 /* Call the callback with all arguments after it to get the loop primed.. */
112 lua_call(L, top - 2, 1); 120 if(lua_pcall(L, top - 2, 1, 0) || !lua_isnumber(L, -1)) {
121 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1));
122 return 0;
123 }
113 ret = lua_tointeger(L, -1); 124 ret = lua_tointeger(L, -1);
114 lua_pop(L, 1); 125 lua_pop(L, 1);
115 if(ret == -1) { /* Done, no need to setup event */ 126 if(ret == -1) { /* Done, no need to setup event */
116 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef); 127 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
117 return 0; 128 return 0;
129 }
130 if(ret != EV_READ && ret != EV_WRITE) {
131 printf("BAD RET_VAL IN INIT: %i\n", ret);
118 } 132 }
119 arg = lua_newuserdata(L, sizeof(*arg)); 133 arg = lua_newuserdata(L, sizeof(*arg));
120 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT); 134 luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT);
121 lua_setmetatable(L, -2); 135 lua_setmetatable(L, -2);
122 136

mercurial