prosody
changeset 5022 776a57ca0d84
parent 4877 6f5b53cb3565
child 5023 dcc8e789df36
equal deleted inserted replaced
5021:85b2689dbcfe 5022:776a57ca0d84
   143 function sandbox_require()
   143 function sandbox_require()
   144 	-- Replace require() with one that doesn't pollute _G, required
   144 	-- Replace require() with one that doesn't pollute _G, required
   145 	-- for neat sandboxing of modules
   145 	-- for neat sandboxing of modules
   146 	local _realG = _G;
   146 	local _realG = _G;
   147 	local _real_require = require;
   147 	local _real_require = require;
       
   148 	if not getfenv then
       
   149 		-- FIXME: This is a hack to replace getfenv() in Lua 5.2
       
   150 		function getfenv(f) return debug.getupvalue(debug.getinfo(f or 1).func, 1); end
       
   151 	end
   148 	function require(...)
   152 	function require(...)
   149 		local curr_env = getfenv(2);
   153 		local curr_env = getfenv(2);
   150 		local curr_env_mt = getmetatable(getfenv(2));
   154 		local curr_env_mt = getmetatable(curr_env);
   151 		local _realG_mt = getmetatable(_realG);
   155 		local _realG_mt = getmetatable(_realG);
   152 		if curr_env_mt and curr_env_mt.__index and not curr_env_mt.__newindex and _realG_mt then
   156 		if curr_env_mt and curr_env_mt.__index and not curr_env_mt.__newindex and _realG_mt then
   153 			local old_newindex, old_index;
   157 			local old_newindex, old_index;
   154 			old_newindex, _realG_mt.__newindex = _realG_mt.__newindex, curr_env;
   158 			old_newindex, _realG_mt.__newindex = _realG_mt.__newindex, curr_env;
   155 			old_index, _realG_mt.__index = _realG_mt.__index, function (_G, k)
   159 			old_index, _realG_mt.__index = _realG_mt.__index, function (_G, k)