lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



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.
Couldn't you override require() to keep track of the current path. In untested-pseudo-lua:

local context_stack = {}

function require(s)
   local context = context_stack[#context_stack]
   local path = resolve_path(s, context)
   if not package.loaded[path]
       table.insert(context_stack, path)
       package.loaded[path] = do_file(path) or true
       context_stack[#context_stack] = nil
   end
end

// Niklas