[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Recipe: setfenv with Lua 5.2.0 work3
- From: GrayFace <sergroj@...>
- Date: Tue, 15 Jun 2010 22:27:31 +0700
Will upvalue 1 work if the loaded file doesn't reference any globals?
On 20.05.2010 23:15, Roberto Ierusalimschy wrote:
With Lua 5.1, I have the following code :
With Lua 5.2.0 work2, I rewrite it as following (compatible with Lua 5.1) :
debug.setfenv(debug.getinfo(level, 'f').func, M)
Now, with Lua 5.2.0 work3, it becomes :
if debug.setupvalue(debug.getinfo(level, 'f').func, 1, M) ~= '_ENV' then
setfenv(level, M) -- Lua 5.1
The value 1 seems magical, but I think _ENV is alway the first upvalue.
Not necessarily. If the function is a main function (that is, the direct
result of load) then _ENV is always the first (and only) upvalue and it
is always present. Other functions have _ENV just like any other upvalue.
For instance, consider the following code:
function foo ()
return a + b
In function foo, the first upvalue is 'a' and the second is '_ENV'.
Sergey Rozhenko mailto:email@example.com