Beginning refactoring of the event_callback outside of the core

Wed, 05 Sep 2007 22:48:49 -0400

author
Thomas Harning Jr <harningt@gmail.com>
date
Wed, 05 Sep 2007 22:48:49 -0400
changeset 20
71bc2e49366c
parent 19
cbaa6e6ef285
child 21
728aafac9682

Beginning refactoring of the event_callback outside of the core

include/event_callback.h file | annotate | diff | comparison | revisions
include/luaevent.h file | annotate | diff | comparison | revisions
src/event_callback.c file | annotate | diff | comparison | revisions
src/luaevent.c file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/event_callback.h	Wed Sep 05 22:48:49 2007 -0400
@@ -0,0 +1,24 @@
+/* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
+ * Licensed as LGPL - See doc/COPYING for details */
+#ifndef EVENT_CALLBACK
+#define EVENT_CALLBACK
+
+#include "luaevent.h"
+#include <lua.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <event.h>
+
+#define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
+
+typedef struct {
+	struct event ev;
+	le_base* base;
+	int callbackRef;
+} le_callback;
+
+int event_callback_register(lua_State* L);
+
+void luaevent_callback(int fd, short event, void* p);
+
+#endif
--- a/include/luaevent.h	Wed Sep 05 22:38:28 2007 -0400
+++ b/include/luaevent.h	Wed Sep 05 22:48:49 2007 -0400
@@ -13,12 +13,6 @@
 	lua_State* loop_L;
 } le_base;
 
-typedef struct {
-	struct event ev;
-	le_base* base;
-	int callbackRef;
-} le_callback;
-
 int luaopen_luaevent(lua_State* L);
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/event_callback.c	Wed Sep 05 22:48:49 2007 -0400
@@ -0,0 +1,56 @@
+#include "event_callback.h"
+#include <assert.h>
+#include <lauxlib.h>
+
+void freeCallbackArgs(le_callback* arg, lua_State* L) {
+	if(arg->base) {
+		arg->base = NULL;
+		event_del(&arg->ev);
+		luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
+	}
+}
+/* le_callback is allocated at the beginning of the coroutine in which it
+is used, no need to manually de-allocate */
+
+/* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
+void luaevent_callback(int fd, short event, void* p) {
+	le_callback* arg = p;
+	lua_State* L;
+	int ret;
+	assert(arg && arg->base && arg->base->loop_L);
+	L = arg->base->loop_L;
+	lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
+	lua_pushinteger(L, event);
+	lua_call(L, 1, 1);
+	ret = lua_tointeger(L, -1);
+	lua_pop(L, 1);
+	if(ret == -1) {
+		freeCallbackArgs(arg, L);
+	} 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);
+		}
+	}
+}
+
+static int luaevent_cb_gc(lua_State* L) {
+	le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
+	freeCallbackArgs(arg, L);
+	return 0;
+}
+
+int event_callback_register(lua_State* L) {
+	luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
+	lua_pushcfunction(L, luaevent_cb_gc);
+	lua_setfield(L, -2, "__gc");
+	lua_newtable(L);
+	lua_pushcfunction(L, luaevent_cb_gc);
+	lua_setfield(L, -2, "close");
+	lua_setfield(L, -2, "__index");
+	lua_pop(L, 1);
+	return 0;
+}
--- a/src/luaevent.c	Wed Sep 05 22:38:28 2007 -0400
+++ b/src/luaevent.c	Wed Sep 05 22:48:49 2007 -0400
@@ -2,13 +2,13 @@
  * Licensed as LGPL - See doc/COPYING for details */
 
 #include "luaevent.h"
+#include "event_callback.h"
 
 #include <lua.h>
 #include <lauxlib.h>
 #include <assert.h>
 
 #define EVENT_BASE_MT "EVENT_BASE_MT"
-#define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT"
 
 int luaevent_newbase(lua_State* L) {
 	le_base *base = (le_base*)lua_newuserdata(L, sizeof(le_base));
@@ -19,41 +19,6 @@
 	return 1;
 }
 
-void freeCallbackArgs(le_callback* arg, lua_State* L) {
-	if(arg->base) {
-		arg->base = NULL;
-		event_del(&arg->ev);
-		luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef);
-	}
-}
-/* le_callback is allocated at the beginning of the coroutine in which it
-is used, no need to manually de-allocate */
-
-/* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */
-static void luaevent_callback(int fd, short event, void* p) {
-	le_callback* arg = p;
-	lua_State* L;
-	int ret;
-	assert(arg && arg->base && arg->base->loop_L);
-	L = arg->base->loop_L;
-	lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef);
-	lua_pushinteger(L, event);
-	lua_call(L, 1, 1);
-	ret = lua_tointeger(L, -1);
-	lua_pop(L, 1);
-	if(ret == -1) {
-		freeCallbackArgs(arg, L);
-	} 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);
-		}
-	}
-}
-
 static int luaevent_base_gc(lua_State* L) {
 	le_base *base = luaL_checkudata(L, 1, EVENT_BASE_MT);
 	if(base->base) {
@@ -63,12 +28,6 @@
 	return 0;
 }
 
-static int luaevent_cb_gc(lua_State* L) {
-	le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT);
-	freeCallbackArgs(arg, L);
-	return 0;
-}
-
 int getSocketFd(lua_State* L, int idx) {
 	int fd;
 	luaL_checktype(L, idx, LUA_TUSERDATA);
@@ -146,6 +105,8 @@
 
 /* Verified ok */
 int luaopen_luaevent_core(lua_State* L) {
+	/* Register external items */
+	event_callback_register(L);
 	/* Setup metatable */
 	luaL_newmetatable(L, EVENT_BASE_MT);
 	lua_newtable(L);
@@ -154,14 +115,6 @@
 	lua_pushcfunction(L, luaevent_base_gc);
 	lua_setfield(L, -2, "__gc");
 	lua_pop(L, 1);
-	luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT);
-	lua_pushcfunction(L, luaevent_cb_gc);
-	lua_setfield(L, -2, "__gc");
-	lua_newtable(L);
-	lua_pushcfunction(L, luaevent_cb_gc);
-	lua_setfield(L, -2, "close");
-	lua_setfield(L, -2, "__index");
-	lua_pop(L, 1);
 
 	luaL_register(L, "luaevent.core", funcs);
 	setNamedIntegers(L, consts);

mercurial