lua-users home
lua-l archive

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


On 31/03/2011 14:22, Anthony Howe whispered from the shadows...:
> On 31/03/2011 14:18, Jerome Vuarand whispered from the shadows...:
>> 2011/3/31 Anthony Howe <achowe+lua@snert.com>:
>>> I have a related problem with function environments in 5.1. How to get
>>> function environments to inherit the environment of the caller and NOT
>>> that of where it was defined.
>>
>> Inside your function, you can get the environment of the caller with
>> getfenv, by passing it a number. You can then replace the current
>> function environment with that. For example :
>>
>> function foo()
>>   setfenv(1, getfenv(2))
>>   -- from now on, all globals are accessing the caller's environment
>> end
> 
> But how to do this automatically? The Lua author shouldn't need to do
> this. There must be some other way from C to do this.

I have tried to create a metatable with a custom __index in the master
state that would get inherited by functions when define. The custom
__index would draw on the executing thread's globals before the master
state's, but I couldn't get it to work.

-- 
Anthony C Howe            Skype: SirWumpus                  SnertSoft
                        Twitter: SirWumpus      BarricadeMX & Milters
http://snert.com/      http://nanozen.info/     http://snertsoft.com/