Comparison

core/modulemanager.lua @ 5021:85b2689dbcfe

Eliminate direct setfenv usage
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 08 Jun 2012 05:04:38 +0200
parent 4896:27cda15104f2
child 5123:7c5c86fa552e
comparison
equal deleted inserted replaced
5020:ef1eb65acbba 5021:85b2689dbcfe
16 16
17 local hosts = hosts; 17 local hosts = hosts;
18 local prosody = prosody; 18 local prosody = prosody;
19 19
20 local pcall, xpcall = pcall, xpcall; 20 local pcall, xpcall = pcall, xpcall;
21 local setmetatable, rawget, setfenv = setmetatable, rawget, setfenv; 21 local setmetatable, rawget = setmetatable, rawget;
22 local pairs, type, tostring = pairs, type, tostring; 22 local pairs, type, tostring = pairs, type, tostring;
23 23
24 local debug_traceback = debug.traceback; 24 local debug_traceback = debug.traceback;
25 local unpack, select = unpack, select; 25 local unpack, select = unpack, select;
26 pcall = function(f, ...) 26 pcall = function(f, ...)
150 end 150 end
151 return nil, "global-module-already-loaded"; 151 return nil, "global-module-already-loaded";
152 end 152 end
153 153
154 154
155 local mod, err = pluginloader.load_code(module_name); 155
156 local _log = logger.init(host..":"..module_name);
157 local api_instance = setmetatable({ name = module_name, host = host,
158 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable() }
159 , { __index = api });
160
161 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
162 api_instance.environment = pluginenv;
163
164 local mod, err = pluginloader.load_code(module_name, nil, pluginenv);
156 if not mod then 165 if not mod then
157 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); 166 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil");
158 return nil, err; 167 return nil, err;
159 end 168 end
160 169
161 local _log = logger.init(host..":"..module_name); 170 api_instance.path = err;
162 local api_instance = setmetatable({ name = module_name, host = host, path = err, 171
163 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable() }
164 , { __index = api });
165
166 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
167 api_instance.environment = pluginenv;
168
169 setfenv(mod, pluginenv);
170
171 modulemap[host][module_name] = pluginenv; 172 modulemap[host][module_name] = pluginenv;
172 local ok, err = pcall(mod); 173 local ok, err = pcall(mod);
173 if ok then 174 if ok then
174 -- Call module's "load" 175 -- Call module's "load"
175 if module_has_method(pluginenv, "load") then 176 if module_has_method(pluginenv, "load") then