Hugo, before looking at my suggestion, please take the time to fully understand how Lua module loading works. I plan to throw it all out and replace it with something completely different.
I want all module names to be complete URLs. This removes ambiguity about how and from where the module is loaded. But I will allow custom protocols to allow custom module loading.
Out of everything in the
Package module, I will only keep Package.require and Package.loaded . Everything else will be removed. I will add Package.protocols which will be a map from a URL protocol to a loader function. The loader function will be passed the URL path and should return either the loaded module or nil if it fails.
I currently have some custom protocols for IO like "java" which is misnamed and should be "classpath". So the basic protocols would be "file" and "classpath". To load Java classes, the Java package would add a "class" protocol. To import ArrayList, you would do:
import "class:java.util.ArrayList"
I would have a standard "luan" protocol defined like this:
function Package.protocols.luan(path)
return Package.protocols.classpath( "classpath:luan/modules/" .. path )
end
So to import the luan Table module:
import "luan:Table"
The Web_server module could add a "site" protocol like this:
function serve(dir)
dir = dir.gsub("/$","") -- remove trailing '/' if any
function Package.protocols.site(path)
return Package.protocols.classpath( "file:" .. dir .. path )
end
...
end
Then you could do:
import "site:/private/lib/Template"
My approach is more verbose than Lua but I think it is clearer.