lua-users home
lua-l archive

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


On Mon, 14 Sep 2009 21:44:02 -0400,  David Manura <dm.lua@math2.org> wrote:
>  environment=[[ require("lanes"); return lanes ]]

If anyone (other than I) starts using Darwin, the pattern above will
probably be a common one because it not only allows the re-use of an
existing Lua module, contained in a file of code, but the structure
definition uses 'require' to find that file.  'require' is (a
re-implementation of) the standard Lua 'require' function, so if '
require("lanes") ' works in your code today, then it will work when
used as in the example above.

> Why isn't the native Lua search path sufficient?

It is sufficient, but I was going for 'distinct', in order to
accommodate the scenario in which the Darwin structure writer is
setting up a user environment for a population of users.  Let me
explain.

My own plans for using Lua are to embed it (no surprise) and to allow
users to write their own scripts (again, no surprise).  But how much
power should I give my users?  On the "low power" end of the spectrum,
I could prevent them from doing io and from interacting with the os.
At the other end of the spectrum, "full power" would give the user the
power to write Lua scripts that did anything, including loading
modules from arbitrary places in the file system (and running
os.execute("luarocks ...") -- a scary thought!).

In my project, I need to set up the environment in which user scripts
run.  I can precisely define the user environment using Darwin (in
darwin/initialstructures.lua).  I plan to allow my users to load
modules.

When I create the user environment, I may want my own search path for
loading files that is distinct from the Lua package.path.   That gives
me the opportunity to write structure declarations that load code from
anywhere in the file system, according to how I've set the Darwin
path.  (The Darwin path applies only to files listed in the 'files'
clause.)

I'll load "special" code from a non-standard place to avoid polluting
the namespace of modules that can be loaded from the standard places
on the Lua path.  "Special" code could be, e.g. code that wraps the io
package to restrict the kinds of io that users can do.  But in
reality, it just lets me separate the code that I wrote to "set up the
system" from the code that user scripts will load using 'require'
(really: package.loaders).

My thoughts on this part of the design are still evolving.  I
considered giving Darwin a mirror of Lua's package table (which has a
wonderfully flexible design) in order to provide the full power of
loaders, preload, etc.  In the end, I opted for 'keep it simple'
instead, and just implemented a search path, mainly because my desire
was only to have my "set up the system" code live in a directory that
is not on the Lua search path.

Jim