lua-users home
lua-l archive

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

> On Wed, Jan 5, 2011 at 5:05 PM, Lorenzo Donati
> <> wrote:
> > In doing this I assumed that _ENV is always the first upvalue of a function.
> > Reading 5.2 docs it seems true, but I remember some old post in which
> > Roberto (or maybe Luiz?) said it isn't always like that (but maybe I
> > remember something superseded by newer work versions).
> _ENV is always the first upvalue of a chunk (i.e. a function created
> by passing a piece of non-bytecode source code to lua_load), but this
> is not true for functions in general. Functions in general may or may
> not have an upvalue called _ENV, which may or may not be a shared
> upvalue, and if they do have an _ENV upvalue, it doesn't have to be
> the 1st upvalue.

Moreover, usually the _ENV of all functions defined in the same chunk
is shared, so if you cannot change the _ENV of only one function.

Sergey Rozhenko posted some time ago a pretty good implementation of
setfenv that handled most of those subtleties.

-- Roberto