Changeset

12949:2f61ebcf37c0

prosody.loader: Ensure already loaded modules are found in old and new namespaces Prevents modules being initialized twice, ensuring that require"prosody.util.foo" == require"util.foo"
author Kim Alvefur <zash@zash.se>
date Fri, 17 Mar 2023 15:11:26 +0100
parents 12948:29983f09c913
children 12950:2cb5994e3f94
files loader.lua
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/loader.lua	Fri Mar 17 14:36:02 2023 +0100
+++ b/loader.lua	Fri Mar 17 15:11:26 2023 +0100
@@ -19,3 +19,17 @@
 		end)
 	end
 end
+
+-- Look for already loaded module with or without prefix
+setmetatable(package.loaded, {
+	__index = function(loaded, module_name)
+		local suffix = module_name:match("^prosody%.(.*)$");
+		if suffix then
+			return rawget(loaded, suffix);
+		end
+		local prefixed = rawget(loaded, "prosody." .. module_name);
+		if prefixed ~= nil then
+			return prefixed;
+		end
+	end;
+})