lua-users home
lua-l archive

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


On Mon, 24 May 2010 15:46:10 -0300
Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:

> > t = {[1]=1, 2}
> > return  t[1]
> > 2
> 
> > What is the correct interpretation?
> 
> luac -l -p can help clear in these cases
> 
>         1       [1]     NEWTABLE        0 1 1
>         2       [1]     SETTABLE        0 -2 -2 ; 1 1
>         3       [1]     LOADK           1 -3    ; 2
>         4       [1]     SETLIST         0 1 1   ; 1
>         5       [1]     SETGLOBAL       0 -1    ; t
>         6       [1]     RETURN          0 1
> 
> Note that SETTABLE sets t[1]=1 and then SETLIST sets t[1]=2.

Just to be sure I correctly understand Luiz's answer, here is my interpretation of the bytecode:

(As I imagined) only items with implicit indexes go to the optimized array part of a table (filled with SETLIST). On item read (the RETURN line), if the index happens to be a positive integer, then the array part is first checked by the VM, possibly hiding an overriding item in the regular map part of the table (filled with SETTABLE).

Below the possible cases:

spir@o:~$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t = {[1]=1, [1]=2}	-- both go to regular map part
> return  t[1]
2
> t = {[1]=1, 2}	-- '2' goes to array part at index 1
> = t[1]
2
> t = {1, [1]=2}	-- '2' will be hidden
> return  t[1]
1

I take the opportunity to ask about the numbers before ';' in the bytecode listing. I guess the first one is the index of a local var (since they are stored in an array). But I have no idea about the other numbers.

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com