luaevent/src/luaevent.c

changeset 9
1f3b72ba96c9
parent 8
b134613f6303
child 10
88ce07d62597
equal deleted inserted replaced
8:b134613f6303 9:1f3b72ba96c9
37 static int call_callback_function(lua_State* L, int argCount) { 37 static int call_callback_function(lua_State* L, int argCount) {
38 int ret; 38 int ret;
39 if(lua_pcall(L, argCount, 1, 0) || !(lua_isnil(L, -1) || lua_isnumber(L, -1))) { 39 if(lua_pcall(L, argCount, 1, 0) || !(lua_isnil(L, -1) || lua_isnumber(L, -1))) {
40 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1)); 40 printf("ERROR IN INIT: %s\n", lua_tostring(L, -1));
41 lua_pop(L, 1); 41 lua_pop(L, 1);
42 return 0; 42 return -1;
43 } 43 }
44 /* Lua_isnil returns 1 if the value is nil... */ 44 /* Lua_isnil returns 1 if the value is nil... */
45 ret = lua_tointeger(L, -1) | -lua_isnil(L, -1); 45 ret = lua_tointeger(L, -1) | -lua_isnil(L, -1);
46 lua_pop(L, 1); 46 lua_pop(L, 1);
47 if(ret < 0) { /* Done, no need to setup event */ 47 if(ret < 0) { /* Done, no need to setup event */
48 return 0; 48 return -1;
49 } 49 }
50 printf("WAITING FOR: %i RED: %i WR:%i\n", ret, EV_READ, EV_WRITE);
50 if(ret != EV_READ && ret != EV_WRITE) { 51 if(ret != EV_READ && ret != EV_WRITE) {
51 printf("BAD RET_VAL IN INIT: %i\n", ret); 52 printf("BAD RET_VAL IN INIT: %i\n", ret);
52 } 53 }
53 return 1; 54 return ret;
54 } 55 }
55 56
56 static void luaevent_callback(int fd, short event, void* p); 57 static void luaevent_callback(int fd, short event, void* p);
57 58
58 static void setup_event(le_callback* arg, int fd, short event, int resetEvent) { 59 static void setup_event(le_callback* arg, int fd, short event, int resetEvent) {
72 lua_State* L = arg->L; 73 lua_State* L = arg->L;
73 int ret; 74 int ret;
74 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef); 75 lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
75 lua_pushinteger(L, event); 76 lua_pushinteger(L, event);
76 77
77 if(0 == call_callback_function(L, 1)) { 78 if(-1 == (ret = call_callback_function(L, 1))) {
78 freeCallbackArgs(arg); 79 freeCallbackArgs(arg);
79 return; 80 return;
80 } 81 }
81 82
82 printf("RET VAL: %i\n", ret); 83 printf("RET VAL: %i\n", ret);
140 top = lua_gettop(L); 141 top = lua_gettop(L);
141 /* Preserve the callback function */ 142 /* Preserve the callback function */
142 lua_pushvalue(L, 2); 143 lua_pushvalue(L, 2);
143 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); 144 callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
144 /* Call the callback with all arguments after it to get the loop primed.. */ 145 /* Call the callback with all arguments after it to get the loop primed.. */
145 if(0 == call_callback_function(L, top - 2)) { 146 if(-1 == (ret = call_callback_function(L, top - 2))) {
146 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef); 147 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
147 return 0; 148 return 0;
148 } 149 }
149 150
150 push_new_callback(L, callbackRef, fd, ret); 151 push_new_callback(L, callbackRef, fd, ret);

mercurial