# HG changeset patch # User Thomas Harning Jr # Date 1181525050 0 # Node ID 5999243fab1d2b991bbf4c0ba42324fe4a8d9c1b # Parent 01b3a96ae760738c006077e45b1e241ae23a1a6d * Added some cheap protection code for failures in callback functions. diff -r 01b3a96ae760 -r 5999243fab1d luaevent/luaevent.lua --- a/luaevent/luaevent.lua Mon Jun 11 01:08:59 2007 +0000 +++ b/luaevent/luaevent.lua Mon Jun 11 01:24:10 2007 +0000 @@ -87,6 +87,7 @@ local oldAddEvent = luaevent.core.addevent luaevent.core.addevent = function(...) local item = oldAddEvent(...) + if not item then print("FAILED TO SETUP ITEM") return item end print("SETUP ITEM FOR: ", debug.getmetatable(item).getfd(item)) if not hookedObjectMt then hookedObjectMt = true diff -r 01b3a96ae760 -r 5999243fab1d luaevent/src/luaevent.c --- a/luaevent/src/luaevent.c Mon Jun 11 01:08:59 2007 +0000 +++ b/luaevent/src/luaevent.c Mon Jun 11 01:24:10 2007 +0000 @@ -43,19 +43,27 @@ int ret; lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef); lua_pushinteger(L, event); - lua_call(L, 1, 1); + if(lua_pcall(L, 1, 1, 0) || !lua_isnumber(L, -1)) { + printf("ERROR IN CB: %s\n", lua_tostring(L, -1)); + freeCallbackArgs(arg); + return; + } ret = lua_tointeger(L, -1); lua_pop(L, 1); if(ret == -1) { freeCallbackArgs(arg); - } else { - struct event *ev = &arg->ev; - int newEvent = ret; - if(newEvent != event) { // Need to hook up new event... - event_del(ev); - event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg); - event_add(ev, NULL); - } + return; + } + if(ret != EV_READ && ret != EV_WRITE) { + printf("BAD RET_VAL: %i\n", ret); + } + + struct event *ev = &arg->ev; + int newEvent = ret; + if(newEvent != event) { // Need to hook up new event... + event_del(ev); + event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg); + event_add(ev, NULL); } } @@ -109,13 +117,19 @@ callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); /* Call the callback with all arguments after it to get the loop primed.. */ - lua_call(L, top - 2, 1); + if(lua_pcall(L, top - 2, 1, 0) || !lua_isnumber(L, -1)) { + printf("ERROR IN INIT: %s\n", lua_tostring(L, -1)); + return 0; + } ret = lua_tointeger(L, -1); lua_pop(L, 1); if(ret == -1) { /* Done, no need to setup event */ luaL_unref(L, LUA_REGISTRYINDEX, callbackRef); return 0; } + if(ret != EV_READ && ret != EV_WRITE) { + printf("BAD RET_VAL IN INIT: %i\n", ret); + } arg = lua_newuserdata(L, sizeof(*arg)); luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT); lua_setmetatable(L, -2);