Comparison

core/configmanager.lua @ 5021:85b2689dbcfe

Eliminate direct setfenv usage
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 08 Jun 2012 05:04:38 +0200
parent 4530:40905e7bf680
child 5124:a4a74a0e9b9c
comparison
equal deleted inserted replaced
5020:ef1eb65acbba 5021:85b2689dbcfe
11 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table; 11 setmetatable, loadfile, pcall, rawget, rawset, io, error, dofile, type, pairs, table;
12 local format, math_max = string.format, math.max; 12 local format, math_max = string.format, math.max;
13 13
14 local fire_event = prosody and prosody.events.fire_event or function () end; 14 local fire_event = prosody and prosody.events.fire_event or function () end;
15 15
16 local envload = require"util.envload".envload;
16 local lfs = require "lfs"; 17 local lfs = require "lfs";
17 local path_sep = package.config:sub(1,1); 18 local path_sep = package.config:sub(1,1);
18 19
19 module "configmanager" 20 module "configmanager"
20 21
162 return p; 163 return p;
163 end 164 end
164 165
165 -- Built-in Lua parser 166 -- Built-in Lua parser
166 do 167 do
167 local loadstring, pcall, setmetatable = _G.loadstring, _G.pcall, _G.setmetatable; 168 local pcall, setmetatable = _G.pcall, _G.setmetatable;
168 local setfenv, rawget, tostring = _G.setfenv, _G.rawget, _G.tostring; 169 local rawget, tostring = _G.rawget, _G.tostring;
169 parsers.lua = {}; 170 parsers.lua = {};
170 function parsers.lua.load(data, config_file, config) 171 function parsers.lua.load(data, config_file, config)
171 local env; 172 local env;
172 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below 173 -- The ' = true' are needed so as not to set off __newindex when we assign the functions below
173 env = setmetatable({ 174 env = setmetatable({
261 262
262 function env.RunScript(file) 263 function env.RunScript(file)
263 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file)); 264 return dofile(resolve_relative_path(config_file:gsub("[^"..path_sep.."]+$", ""), file));
264 end 265 end
265 266
266 local chunk, err = loadstring(data, "@"..config_file); 267 local chunk, err = envload(data, "@"..config_file, env);
267 268
268 if not chunk then 269 if not chunk then
269 return nil, err; 270 return nil, err;
270 end 271 end
271 272
272 setfenv(chunk, env);
273
274 local ok, err = pcall(chunk); 273 local ok, err = pcall(chunk);
275 274
276 if not ok then 275 if not ok then
277 return nil, err; 276 return nil, err;
278 end 277 end