lua-users home
lua-l archive

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


On Jan 9, 2010, at 7:37 AM, David Manura wrote:

> On Sat, Jan 9, 2010 at 6:29 AM, steve donovan wrote:
>> However, (and this is a nasty) there are some tricks you cannot do
>> with debug.setfenv.
>> 
>> Consider this implementation of David Manura's 'module environment ~=
>> module table' idea:  [package.clean] ....
> 
> http://lua-users.org/wiki/ModuleDefinition
> 
>>    debug.setfenv(3,env)  .....
>> ../lua: ./closed.lua:12: 'setfenv' cannot change environment of given object
> 
> In 5.1, setfenv can take a stack level number but debug.setfenv can
> not.  debug.setfenv can take a userdata but setfenv could not.  One is
> not a subset of the other.  Now, in 5.1 you can alternately obtain the
> function at a given stack level number like debug.getinfo(3, 'f').func
> and then call setfenv or debug.setenv on it to achieve the same effect
> to setenv with a stack level number.  In Lua 5.2.0-work1, this
> functionality is maintained, though in the debug library.

Wow. I'm sure the 5.1 spec said this, but I hadn't appreciated that setfenv with a numeric parameter actually changes the environment of the function at that spot on the stack rather than an environment reference in the activation record.

The fact that debug.setenv can muck with the environment for userdata does not strike me as a good thing if it is to be the replacement for the old setfenv.

Mark