Comparison

core/modulemanager.lua @ 4606:17785dbd9d58

modulemanager: Some refactoring. Deprecate module.host = "*", modules should call module:set_global() (which has been around since forever)
author Matthew Wild <mwild1@gmail.com>
date Wed, 14 Mar 2012 21:37:00 +0000
parent 4604:eef5e3a83792
child 4638:352cd61e2682
comparison
equal deleted inserted replaced
4605:69746db53125 4606:17785dbd9d58
121 return nil, "unknown-host"; 121 return nil, "unknown-host";
122 end 122 end
123 123
124 if not modulemap[host] then 124 if not modulemap[host] then
125 modulemap[host] = {}; 125 modulemap[host] = {};
126 hosts[host].modules = modulemap[host];
126 end 127 end
127 128
128 if modulemap[host][module_name] then 129 if modulemap[host][module_name] then
129 log("warn", "%s is already loaded for %s, so not loading again", module_name, host); 130 log("warn", "%s is already loaded for %s, so not loading again", module_name, host);
130 return nil, "module-already-loaded"; 131 return nil, "module-already-loaded";
146 147
147 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); 148 local pluginenv = setmetatable({ module = api_instance }, { __index = _G });
148 api_instance.environment = pluginenv; 149 api_instance.environment = pluginenv;
149 150
150 setfenv(mod, pluginenv); 151 setfenv(mod, pluginenv);
151 hosts[host].modules = modulemap[host];
152 modulemap[host][module_name] = pluginenv;
153 152
154 local ok, err = pcall(mod); 153 local ok, err = pcall(mod);
155 if ok then 154 if ok then
156 -- Call module's "load" 155 -- Call module's "load"
157 if module_has_method(pluginenv, "load") then 156 if module_has_method(pluginenv, "load") then
159 if not ok then 158 if not ok then
160 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil"); 159 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
161 end 160 end
162 end 161 end
163 162
164 -- Use modified host, if the module set one 163 modulemap[pluginenv.module.host][module_name] = pluginenv;
165 if api_instance.host == "*" and host ~= "*" then 164 if pluginenv.module.host == "*" then
166 modulemap[host][module_name] = nil; 165 if not pluginenv.module.global then -- COMPAT w/pre-0.9
167 modulemap["*"][module_name] = pluginenv; 166 log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name);
168 api_instance:set_global(); 167 api_instance:set_global();
169 end 168 end
170 else 169 else
170 hosts[host].modules[module_name] = pluginenv;
171 end
172 end
173 if not ok then
171 log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil"); 174 log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
172 do_unload_module(api_instance.host, module_name); -- Ignore error, module may be partially-loaded
173 end 175 end
174 return ok and pluginenv, err; 176 return ok and pluginenv, err;
175 end 177 end
176 178
177 local function do_reload_module(host, name) 179 local function do_reload_module(host, name)