[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: require()
- From: "Jay Carlson" <nop@...>
- Date: Sat, 25 Jan 2003 11:14:46 +0900
"Wim Couwenberg" <w.couwenberg@chello.nl> writes:
> Hi,
>
> > I know I can arrange an own
> > solution to suppress both behaviours (patch require or check if a
> > file is already slowly dofiled) but I feel that require() should do this
> > alone. Now require isn't so much usefull as it could be. And I don't see
> > arguments to let require() as it is.
>
> Well circular module dependencies _may_ cause trouble and can therefore
not
> easily be supported in general. In some cases, the following might work.
> Mark the module as loaded (in the global _LOADED table) just before
> "luaB_require" runs the module (in lbaselib.c, ln 480 for lua5.0b) instead
> of afterwards. This prevents cyclic loading of packages. If the modules
> involved do not actually use any other module's variables in their
_global_
> scope then this will do the trick. In other cases the behavior may be
> unpredictable... [!]
This is what I did in my require() implementation for 4.0, by analogy to
this C idiom:
#ifndef _FOO_H
#define _FOO_H
#include <bar.h>
#include <baz.h>
/* foo.h body goes here */
#endif
If nested includes call for this header, it will only be included once. This
seems natural to C people, at least....
By the way, it can be quite useful to have access to the body of the _LOADED
table; at some point, this was not accessible to Lua code. I'm too lazy to
look at 5.0 right now.
Jay