[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Explanation needed for setfenv replacement for Lua 5.2
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 4 May 2011 18:46:01 -0300
> Please, consider the following toy code:
>
> [...]
This is the kind of use that deserves the use of the debug library,
because you explicitly want to break the usual visibility rules.
Note that, for this particular use, you do not need the entire
apparatus of Rhozenko's code. (In particular, you do not need the part
you had not understood.) A simpler 'setfenv' will do:
-- not tested!
setfenv = function(f, t)
for up = 1, math.huge do
local name = debug.getupvalue(f, up)
if name == '_ENV' then
debug.setupvalue(f, up, t)
return
elseif name == nil then
return
end
end
end
-- Roberto