|
On 6 Oct 2006, at 07:16, Glenn Maynard wrote:
I'm planning on having Lua files scattered throughout a data tree, alongside the assets they're related to. I want to be able to load other Lua files by both absolute and relative paths, relative to the script loading it. One way of doing this seems to be to pull the source path (of the caller of whichever helper is being used to load) from debug info. (If it's not a @path, only allow absolute paths.) This suffers from using the debug API for non-debug tasks, and also requires being more careful about purging debug info. I can't think of any other way of doing this, though. Access to the debug interface would be hidden away in C, so I could still restrict access to "debug" in Lua.
You could modify the loader so that some sort of reference (which need not always be a pathname, but let's simplify and say it is) to the module's location was passed to the module when it was 'require'ed. The module could then add this location to its loadpath before 'require'ing anything. Automatically, natch. In fact, I don't see why you need to pass the module location to the module, the modified package system can just set it up so that it works behind the scenes.
Any better approaches, or gotchas that I'm not thinking of? I'd expectthis to be a common requirement for larger codebases; I should be able to move around, rename and copy a directory without breaking internal filename references.
Yes, it sounds pretty useful, like Java's java.lang.Class.getResourceAsStream(String)
drj