aboutsummaryrefslogtreecommitdiffstats
path: root/src/utility.c
diff options
context:
space:
mode:
authorThomas Harning Jr <harningt@gmail.com>2007-09-21 14:35:11 -0400
committerThomas Harning Jr <harningt@gmail.com>2007-09-21 14:35:11 -0400
commite652e93f7d73f67c6bb5299c9df14d42b66dba7d (patch)
tree57a6e77f4efbd2d37404e2ee627e154fe4a7940a /src/utility.c
parent68bd2b507fa935eb491479b10e53a97b17994cb5 (diff)
downloadluaevent-prosody-e652e93f7d73f67c6bb5299c9df14d42b66dba7d.tar.gz
luaevent-prosody-e652e93f7d73f67c6bb5299c9df14d42b66dba7d.zip
buffer_event is born, albeit w/ deformities. Can construct instances but not use, yet.
Diffstat (limited to 'src/utility.c')
-rw-r--r--src/utility.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/utility.c b/src/utility.c
new file mode 100644
index 0000000..d42be1f
--- /dev/null
+++ b/src/utility.c
@@ -0,0 +1,45 @@
+/* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
+ * Licensed as LGPL - See doc/COPYING for details */
+#include "utility.h"
+#include <lauxlib.h>
+
+#define WEAK_REF_LOCATION le_register_utility
+
+static void get_weakref_table(lua_State* L) {
+ lua_pushlightuserdata(L, WEAK_REF_LOCATION);
+ lua_gettable(L, LUA_REGISTRYINDEX);
+}
+
+void le_weak_ref(lua_State* L, void* ptr, int idx) {
+ get_weakref_table(L);
+ lua_pushlightuserdata(L, ptr);
+ if(idx < 0) idx-=2;
+ lua_pushvalue(L, idx);
+ lua_settable(L, -3);
+}
+void le_weak_unref(lua_State* L, void* ptr) {
+ get_weakref_table(L);
+ lua_pushlightuserdata(L, ptr);
+ lua_pushnil(L);
+ lua_settable(L, -3);
+}
+
+void le_weak_get(lua_State* L, void* ptr) {
+ get_weakref_table(L);
+ lua_pushlightuserdata(L, ptr);
+ lua_gettable(L, -2);
+}
+
+static void push_weak_table(lua_State* L, const char* mode) {
+ lua_newtable(L);
+ lua_createtable(L,0,1);
+ lua_pushstring(L,mode);
+ lua_setfield(L,-2,"__mode");
+ lua_setmetatable(L,-2);
+}
+
+void le_register_utility(lua_State* L) {
+ lua_pushlightuserdata(L, WEAK_REF_LOCATION);
+ push_weak_table(L, "v");
+ lua_settable(L, LUA_REGISTRYINDEX);
+}