lua-users home
lua-l archive

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


That is very pretty.

I assume when you say "exports one or more named values" that you mean that it exports them to the dictionary import() uses? In that
case, you could even make import the dictionary, instead of a
function, and define an __index metamethod.

That would make the implementation something like this, leaving out
the interesting parts (like the bundle-finder and the bundle-runner):

import = setmetatable({}, {__index =
  function(self, name)
    local bundle, bundlename = findBundleForName(name)
    setfenv(bundle, setmetatable({}, {__index = getfenv(0)})
    local exports = bundle()
    for k, v in exports do
      if rawget(self, k) == nil then self[k] = v end
    end
    local rv = rawget(self, name)
    if rv == nil then
      error(bundlename .. " did not export " .. name)
    end
    return rv
  end})

-------

local showRect = import.showRect

-------


On 24-Jul-04, at 7:36 PM, Mark Hamburg wrote:

FYI...

On my project we've implemented a modified version of require based around a notion of code packaging. In the case of MacOS X, these are bundles. Every bundle gets it's own environment that inherits from the global environment. That way multiple scripts and C code in the same bundle can see each other,
but they don't stomp the rest of the environment.

We've also built an import mechanism in which one issues a call:

    import "name"

This looks up "name" in a dictionary and returns the value that's there if
it already exists and invokes code to go find something to load if it
doesn't. The loaded code then exports one or more named values (generally tables). This relies on having metadata associated with loadable entities
indicating what names they can supply.

"require" is mostly used as part of loading pieces of an individual module
while "import" is used to invoke the loading of modules.

Mark