lua-users home
lua-l archive

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

The Lua compiler does two things:

1) introduce a local variable _ENV just outside the scope of the
'chunk' (string or file) it compiles and set its initial value to the
4th argument of 'load', or to the global environment if that argument
is missing. So the result of compiling a chunk of code is a closure
that has access to _ENV, which is initialized to a value provided at
compile time.

2) translate any unbound variable reference to an table access, e.g.
the identifier "foo" translates to This is a regular table

That's all. You are free to introduce your own _ENV variable, or
assign to any _ENV variable. References to _ENV in your code are in no
way special, except that its always bound.

Note that the default behavior of 'load' and 'loadfile' is to set _ENV
to the 'standard' globals table, but you can set it to whatever you

translates to:
or equivalently:

To give a module its own global variable table you can reassign _ENV:

  _ENV = {a = 1, b = 2, print = G.print}

  print(a, b) -- should print 1 and 2