prosody: Set metatable on functions to allow easy access to upvalues.
authorWaqas Hussain <waqas20@gmail.com>
Mon, 19 Apr 2010 13:28:12 +0000 (18:28 +0500)
committerWaqas Hussain <waqas20@gmail.com>
Mon, 19 Apr 2010 13:28:12 +0000 (18:28 +0500)
prosody

diff --git a/prosody b/prosody
index 0f705b62bc905cd4f1019b6cf049f7dede10f54c..c7f91456d7d66f3f27b7ad16e7591bc942a76d58 100755 (executable)
--- a/prosody
+++ b/prosody
@@ -22,6 +22,9 @@ if CFG_SOURCEDIR then
        package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
 end
 
+package.path = package.path..";"..(CFG_SOURCEDIR or ".").."/fallbacks/?.lua";
+package.cpath = package.cpath..";"..(CFG_SOURCEDIR or ".").."/fallbacks/?.so";
+
 -- Substitute ~ with path to home directory in data path
 if CFG_DATADIR then
        if os.getenv("HOME") then
@@ -120,6 +123,29 @@ function sandbox_require()
        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 = {};
@@ -415,6 +441,7 @@ read_config();
 init_logging();
 check_dependencies();
 sandbox_require();
+set_function_metatable();
 load_libraries();
 init_global_state();
 read_version();