[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: a case for multiple environments - expressing modules more naturally
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 10 Aug 2010 14:23:12 -0300
> > Although I may not be understanding all the details of how _ENV works...
> You don't have to use _ENV if you don't want to, of course (that's one
> nice thing about using standard mechanisms rather than "magic" ones).
Currently we see _ENV more as a simple way to define how globals work
(and the dependency between a chunk and the global table) than as a
mechanism for module construction.
Nevertheless, I should mention that there are very few details of
how _ENV works:
- all chunks (the unit of 'loading' in Lua) are compiled as if they
were a function defined under a "local _ENV" declaration. (In 5.1,
all chunks are already compiled as if they were a function; the only
novelty is this upvalue _ENV.)
- all references to undeclared variables are syntactically translated to
_ENV.varname. ("syntactically" means that the compiler rewrites 'x' as
'_ENV.x' and follows from there.)
- when a chunk is loaded with 'load' and the like (loadstring, loadfile,
dofile, etc.), its external _ENV variable gets the value of the global
table. When a chunk is loaded with 'loadin', _ENV gets the value
given as argument.
That is all there is to understand about _ENV.