Diff

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
line wrap: on
line diff
--- a/core/modulemanager.lua	Sun Oct 18 03:15:33 2009 +0500
+++ b/core/modulemanager.lua	Sun Oct 18 05:17:07 2009 +0500
@@ -135,28 +135,32 @@
 		log("debug", "Created new component: %s", host);
 	end
 	hosts[host].modules = modulemap[host];
-	
-	local success, ret = pcall(mod);
-	if not success then
-		log("error", "Error initialising module '%s': %s", module_name or "nil", ret or "nil");
-		return nil, ret;
-	end
+	modulemap[host][module_name] = pluginenv;
 	
-	if module_has_method(pluginenv, "load") then
-		local ok, err = call_module_method(pluginenv, "load");
-		if (not ok) and err then
-			log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err);
+	local success, err = pcall(mod);
+	if success then
+		if module_has_method(pluginenv, "load") then
+			success, err = call_module_method(pluginenv, "load");
+			if not success then
+				log("warn", "Error loading module '%s' on '%s': %s", module_name, host, err or "nil");
+			end
 		end
-	end
 
-	-- Use modified host, if the module set one
-	modulemap[api_instance.host][module_name] = pluginenv;
-	
-	if api_instance.host == "*" and host ~= "*" then
-		api_instance:set_global();
+		-- Use modified host, if the module set one
+		if api_instance.host == "*" and host ~= "*" then
+			modulemap[host][module_name] = nil;
+			modulemap["*"][module_name] = pluginenv;
+			api_instance:set_global();
+		end
+	else
+		log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
 	end
-		
-	return true;
+	if success then
+		return true;
+	else -- load failed, unloading
+		unload(api_instance.host, module_name);
+		return nil, err;
+	end
 end
 
 function get_module(host, name)