From c5e71fedaad9115c235f5b72f6a3894da264d18d Mon Sep 17 00:00:00 2001 From: Thomas Harning Jr Date: Mon, 11 Jun 2007 01:24:10 +0000 Subject: [PATCH] * Added some cheap protection code for failures in callback functions. --- luaevent/luaevent.lua | 1 + luaevent/src/luaevent.c | 34 ++++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/luaevent/luaevent.lua b/luaevent/luaevent.lua index be7f296..6e6cf05 100644 --- a/luaevent/luaevent.lua +++ b/luaevent/luaevent.lua @@ -87,6 +87,7 @@ end 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 --git a/luaevent/src/luaevent.c b/luaevent/src/luaevent.c index d79345e..120d433 100644 --- a/luaevent/src/luaevent.c +++ b/luaevent/src/luaevent.c @@ -43,19 +43,27 @@ static void luaevent_callback(int fd, short event, void* p) { 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 @@ static int luaevent_addevent(lua_State* L) { 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); -- 2.11.4.GIT