* Added some cheap protection code for failures in callback

Mon, 11 Jun 2007 01:24:10 +0000

author
Thomas Harning Jr <harningt@gmail.com>
date
Mon, 11 Jun 2007 01:24:10 +0000
changeset 3
5999243fab1d
parent 2
01b3a96ae760
child 4
4d0e9388214a

* Added some cheap protection code for failures in callback
functions.

luaevent/luaevent.lua file | annotate | diff | comparison | revisions
luaevent/src/luaevent.c file | annotate | diff | comparison | revisions
--- 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
--- 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);

mercurial