Software / code / prosody
Comparison
core/modulemanager.lua @ 4776:dbe9d75c0452
modulemanager: Fixes to handle circular dependencies in module:depends()
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 28 Apr 2012 15:47:43 +0100 |
| parent | 4746:37020f4b6799 |
| child | 4801:83cedf648b46 |
comparison
equal
deleted
inserted
replaced
| 4775:ab73a32a655e | 4776:dbe9d75c0452 |
|---|---|
| 137 },{ | 137 },{ |
| 138 __index = modulemap["*"][module_name].module; | 138 __index = modulemap["*"][module_name].module; |
| 139 }); | 139 }); |
| 140 local host_module = setmetatable({ module = host_module_api }, { __index = mod }); | 140 local host_module = setmetatable({ module = host_module_api }, { __index = mod }); |
| 141 host_module_api.environment = host_module; | 141 host_module_api.environment = host_module; |
| 142 modulemap[host][module_name] = host_module; | |
| 142 local ok, result, module_err = call_module_method(mod, "add_host", host_module_api); | 143 local ok, result, module_err = call_module_method(mod, "add_host", host_module_api); |
| 143 if not ok or result == false then return nil, ok and module_err or result; end | 144 if not ok or result == false then |
| 144 modulemap[host][module_name] = host_module; | 145 modulemap[host][module_name] = nil; |
| 146 return nil, ok and module_err or result; | |
| 147 end | |
| 145 return host_module; | 148 return host_module; |
| 146 end | 149 end |
| 147 return nil, "global-module-already-loaded"; | 150 return nil, "global-module-already-loaded"; |
| 148 end | 151 end |
| 149 | 152 |
| 162 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); | 165 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); |
| 163 api_instance.environment = pluginenv; | 166 api_instance.environment = pluginenv; |
| 164 | 167 |
| 165 setfenv(mod, pluginenv); | 168 setfenv(mod, pluginenv); |
| 166 | 169 |
| 170 modulemap[host][module_name] = pluginenv; | |
| 167 local ok, err = pcall(mod); | 171 local ok, err = pcall(mod); |
| 168 if ok then | 172 if ok then |
| 169 -- Call module's "load" | 173 -- Call module's "load" |
| 170 if module_has_method(pluginenv, "load") then | 174 if module_has_method(pluginenv, "load") then |
| 171 ok, err = call_module_method(pluginenv, "load"); | 175 ok, err = call_module_method(pluginenv, "load"); |
| 172 if not ok then | 176 if not ok then |
| 173 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil"); | 177 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil"); |
| 174 end | 178 end |
| 175 end | 179 end |
| 176 | 180 |
| 177 modulemap[api_instance.host][module_name] = pluginenv; | |
| 178 if api_instance.host == "*" then | 181 if api_instance.host == "*" then |
| 179 if not api_instance.global then -- COMPAT w/pre-0.9 | 182 if not api_instance.global then -- COMPAT w/pre-0.9 |
| 180 log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name); | 183 log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name); |
| 181 api_instance:set_global(); | 184 api_instance:set_global(); |
| 182 end | 185 end |
| 186 modulemap[host][module_name] = nil; | |
| 187 modulemap[api_instance.host][module_name] = pluginenv; | |
| 183 if host ~= api_instance.host and module_has_method(pluginenv, "add_host") then | 188 if host ~= api_instance.host and module_has_method(pluginenv, "add_host") then |
| 184 -- Now load the module again onto the host it was originally being loaded on | 189 -- Now load the module again onto the host it was originally being loaded on |
| 185 ok, err = do_load_module(host, module_name); | 190 ok, err = do_load_module(host, module_name); |
| 186 end | 191 end |
| 187 end | 192 end |
| 188 end | 193 end |
| 189 if not ok then | 194 if not ok then |
| 195 modulemap[api_instance.host][module_name] = nil; | |
| 190 log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil"); | 196 log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil"); |
| 191 end | 197 end |
| 192 return ok and pluginenv, err; | 198 return ok and pluginenv, err; |
| 193 end | 199 end |
| 194 | 200 |