Comparison

prosody @ 6470:67501b5576d3

prosody: Make getfenv() replacement for require() sandboxing local to avoid polluting the globals table
author Kim Alvefur <zash@zash.se>
date Mon, 13 Oct 2014 21:02:04 +0200
parent 6416:9af742bb45b2
child 6554:6c22bec3e8d0
comparison
equal deleted inserted replaced
6468:3728c30da4e3 6470:67501b5576d3
149 function sandbox_require() 149 function sandbox_require()
150 -- Replace require() with one that doesn't pollute _G, required 150 -- Replace require() with one that doesn't pollute _G, required
151 -- for neat sandboxing of modules 151 -- for neat sandboxing of modules
152 local _realG = _G; 152 local _realG = _G;
153 local _real_require = require; 153 local _real_require = require;
154 if not getfenv then 154 local getfenv = getfenv or function (f)
155 -- FIXME: This is a hack to replace getfenv() in Lua 5.2 155 -- FIXME: This is a hack to replace getfenv() in Lua 5.2
156 function getfenv(f) 156 local name, env = debug.getupvalue(debug.getinfo(f or 1).func, 1);
157 local name, env = debug.getupvalue(debug.getinfo(f or 1).func, 1); 157 if name == "_ENV" then
158 if name == "_ENV" then 158 return env;
159 return env;
160 end
161 end 159 end
162 end 160 end
163 function require(...) 161 function require(...)
164 local curr_env = getfenv(2); 162 local curr_env = getfenv(2);
165 local curr_env_mt = getmetatable(curr_env); 163 local curr_env_mt = getmetatable(curr_env);