lua-users home
lua-l archive

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


> > 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.

-- Roberto