lua-users home
lua-l archive

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


> On Mar 27, 2015, at 2:28 PM, Sean Conner <sean@conman.org> wrote:
> 
>  I would expect setting the first upvalue of meta.print() to meta would
> make meta the default global environment for meta.print() and not replace
> the current global environment.
> 

Actually, the compiler makes no guarantees about which upvalue (if any) for a function contains the _ENV environment EXCEPT in the case of the implicit anonymous function that wraps a compiled chunk. The load() API family always sets the first upvalue to the environment because it assumes that the chunk being loaded was a compiled chunk (rather than an explicit function within a chunk that was accessed with string.dump().

So you don’t know that the first upvalue of meta.print is the _ENV upvalue. You can look at the upvalue names for “_ENV", but that is not available on stripped bytecode chunks.

I would have preferred the compiler emitted  a index indicating which (if any) of the upvalues was the _ENV, and load() setting this upvalue rather than blindly setting the first and hoping that it was correct, though I’m sure Roberto/Luiz had a good reason for not doing this (my guess is wanting to avoid making “_ENV” special).

—Tim