[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Simulating getfenv for Lua 5.2
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 9 Jun 2010 12:10:16 -0300
> We're considering moving to Lua 5.2 upon release. While it's looking
> promising, the biggest hurdle for us is the abolition of getfenv.
>
> We're using getfenv to simulate Python's package system, such that:
>
> import 'hello.world'
>
> imports a lua file into the variable 'world' of _this_ environment.
If 'import' is only called from the main function in a module, then you
can safely assume that the environment will be the first upvalue of the
callee function. So, you can use debug.getupvalue to get its value:
function import(fname)
...
local this_env = {}
loadin(fname, this_env)
local _, calling_env = debug.getupvalue(debug.getinfo(2).func, 1)
assert(_ == "_ENV")
calling_env['world'] = this_env
end
(Untested code...)
-- Roberto