Diff

prosody @ 2976:15c056c1d9eb

prosody: Set metatable on functions to allow easy access to upvalues.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 19 Apr 2010 18:28:12 +0500
parent 2925:692b3c6c5bd2
child 2985:fde53d82fde0
child 3023:454e1cf18daf
line wrap: on
line diff
--- a/prosody	Sun Apr 11 01:29:59 2010 +0100
+++ b/prosody	Mon Apr 19 18:28:12 2010 +0500
@@ -123,6 +123,29 @@
 	end
 end
 
+function set_function_metatable()
+	local mt = {};
+	function mt.__index(f, upvalue)
+		local i, name, value = 0;
+		repeat
+			i = i + 1;
+			name, value = debug.getupvalue(f, i);
+		until name == upvalue or name == nil;
+		return value;
+	end
+	function mt.__newindex(f, upvalue, value)
+		local i, name = 0;
+		repeat
+			i = i + 1;
+			name = debug.getupvalue(f, i);
+		until name == upvalue or name == nil;
+		if name then
+			debug.setupvalue(f, i, value);
+		end
+	end
+	debug.setmetatable(function() end, mt);
+end
+
 function init_global_state()
 	bare_sessions = {};
 	full_sessions = {};
@@ -418,6 +441,7 @@
 init_logging();
 check_dependencies();
 sandbox_require();
+set_function_metatable();
 load_libraries();
 init_global_state();
 read_version();