Comparison

core/modulemanager.lua @ 1994:9cc9b096c8f5

modulemanager: Module loading rewritten to gracefully deal with errors in module initialization.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 18 Oct 2009 05:17:07 +0500
parent 1993:09621f8d0366
child 2072:464a5392bc80
comparison
equal deleted inserted replaced
1993:09621f8d0366 1994:9cc9b096c8f5
133 hosts[host] = create_component(host); 133 hosts[host] = create_component(host);
134 hosts[host].connected = false; 134 hosts[host].connected = false;
135 log("debug", "Created new component: %s", host); 135 log("debug", "Created new component: %s", host);
136 end 136 end
137 hosts[host].modules = modulemap[host]; 137 hosts[host].modules = modulemap[host];
138 138 modulemap[host][module_name] = pluginenv;
139 local success, ret = pcall(mod); 139
140 if not success then 140 local success, err = pcall(mod);
141 log("error", "Error initialising module '%s': %s", module_name or "nil", ret or "nil"); 141 if success then
142 return nil, ret; 142 if module_has_method(pluginenv, "load") then
143 end 143 success, err = call_module_method(pluginenv, "load");
144 144 if not success then
145 if module_has_method(pluginenv, "load") then 145 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
146 local ok, err = call_module_method(pluginenv, "load"); 146 end
147 if (not ok) and err then 147 end
148 log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err); 148
149 end 149 -- Use modified host, if the module set one
150 end 150 if api_instance.host == "*" and host ~= "*" then
151 151 modulemap[host][module_name] = nil;
152 -- Use modified host, if the module set one 152 modulemap["*"][module_name] = pluginenv;
153 modulemap[api_instance.host][module_name] = pluginenv; 153 api_instance:set_global();
154 154 end
155 if api_instance.host == "*" and host ~= "*" then 155 else
156 api_instance:set_global(); 156 log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
157 end 157 end
158 158 if success then
159 return true; 159 return true;
160 else -- load failed, unloading
161 unload(api_instance.host, module_name);
162 return nil, err;
163 end
160 end 164 end
161 165
162 function get_module(host, name) 166 function get_module(host, name)
163 return modulemap[host] and modulemap[host][name]; 167 return modulemap[host] and modulemap[host][name];
164 end 168 end