lua-users home
lua-l archive

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

Am 02.06.2014 03:10 schröbte Robert Virding:
Reading through the manual I have understood that:

- _ENV is an upvalue to a chunk and not an argument to the chunk function
- It is the only upvalue to the chunk

If you obtained the chunk by parsing Lua source code, then yes.

Am I correct here? There is a slightly cryptic section in the documentation
of load which has me puzzled:

  "If the resulting function has upvalues, the first upvalue is set to the
value of the global environment or to env, if that parameter is given. When
loading main chunks, the first upvalue will be the _ENV variable (see §2.2

You can compile a Lua function with an arbitrary number of upvalues to byte code using `string.dump` and later load it again. The number (but not the values/references) of the upvalues are stored in the byte code. When loaded, the function gets its original number of upvalues (all set to nil). If the loaded function has more than zero upvalues and you passed an env argument to `load`, the first upvalue will be set to that argument. If you didn't pass an env argument, the first upvalue will be set to the globals table iff the loaded function has exactly one upvalue.

I am assuming that the resulting function is the chunk function.