[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Let's not break existing scripts.
- From: Petite Abeille <petite.abeille@...>
- Date: Thu, 10 Nov 2011 19:16:45 +0100
On Nov 10, 2011, at 7:06 PM, Duncan Cross wrote:
> No, no and no. :)
Right, my bad :)
> _ENV is only the first upvalue for a function that was compiled from a
> top-level source code chunk. Functions *in general*, i.e. also
> including functions defined inside a chunk or other function, may have
> _ENV at a different position, or even not at all.
>
> This has been discussed before:
> http://lua-users.org/lists/lua-l/2011-01/msg00377.html
Yes, thanks for the pointer, got confused somewhere along the way :))
FWIW, I use the following rendition of Sergey Rhozenko's original 5.2 setfenv:
http://dev.alt.textdrive.com/browser/HTTP/debugx.lua#L66
function setfenv( aFunction, anEnvironment )
local aFunction = GetFunction( aFunction, 2 )
for anIndex, aName, aValue in UpValues( aFunction ) do
if aName == '_ENV' then
return upvaluejoin( aFunction, anIndex, function() return anEnvironment end, 1 )
end
end
error( 'No _ENV found for ' .. aFunction )
end
Aside from its reliance on debug info, is there anything else subtly broken in that function?
Thanks!