lua-users home
lua-l archive

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


Hi Luiz,

> The manual is clear: "When loading main chunks, the first upvalue will
> be the _ENV variable". The key word here is "main". So, this holds only

I have slightly more convoluted case (with shorter code). I amended
the example in the original question to make _ENV to be the first
upvalue:

function ffactory(x) return function() math.abs(0) return x end end
local f = (loadstring or load)(string.dump(ffactory(5)))
print(f())

Luac output confirms that:

function <upval.lua:1,1> (7 instructions at 002A1468)
upvalues (2) for 002A1468:
	0	_ENV	0	0
	1	x	1	0

But running this code produces an error (Lua 5.2.1):

...\lua.exe: upval.lua:1: attempt to index upvalue '_ENV' (a nil value)
stack traceback:
	upval.lua:1: in function 'f'
	upval.lua:3: in main chunk
	[C]: in ?

Why does this fragment not find the _ENV upvalue?

Paul.

On Fri, Jan 25, 2013 at 2:00 AM, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
>> Running the attached script you'll notice that _ENV is not the first
>> upvalue. Should the manual be amended?
>
> The manual is clear: "When loading main chunks, the first upvalue will
> be the _ENV variable". The key word here is "main". So, this holds only
> for the function that represents a chunk, not the functions that it
> defines. Try luac -l -l on your program and get:
>
> main <upvals.lua:0,0> (33 instructions at 0x1019016f0)
> upvalues (1) for 0x1019016f0:
>         0       _ENV    1       0
>
> function <upvals.lua:3,6> (7 instructions at 0x101901930)
> upvalues (2) for 0x101901930:
>         0       somelocal       1       0
>         1       _ENV    0       0
>