lua-users home
lua-l archive

Re: Table constructor question

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



On 03/01/15 02:00 PM, Zehao Jin wrote:
[     3    [1]    SETTABLE     0 -3 -4    ; 1 "a"
    4    [1]    SETLIST      0 1 1    ; 1
    5    [1]    SETTABUP     0 -1 0    ; _ENV "tb"
    6    [1]    RETURN       0 1
constants (4) for 0x12220c0:
    1    "tb"
    2    "b"
    3    1
    4    "a"
locals (0) for 0x12220c0:
upvalues (1) for 0x12220c0:
    0    _ENV    1    0
[
    3    [1]    LOADK        1 -4    ; "b"
    4    [1]    SETTABLE     0 -2 -5    ; 1 "c"
    5    [1]    SETLIST      0 1 1    ; 1
    6    [1]    SETTABUP     0 -1 0    ; _ENV "tb"
    7    [1]    RETURN       0 1
constants (5) for 0x222d0c0:
    1    "tb"
    2    1
    3    "a"
    4    "b"
    5    "c"
locals (0) for 0x222d0c0:
upvalues (1) for 0x222d0c0:
    0    _ENV    1    0
[
thought tb={ 'b' , [1] = 'a' } would be translated by Lua to tb={ [1] =  'b' , [1] = 'a' } and tb[1] should be 'a',
but that's wrong.

Assigning to the same thing on the same _expression_ is undefined. For example:
$ luac -l -l -
a,a = 1,2

main <stdin:0,0> (4 instructions at 0x222e0c0)
0+ params, 2 slots, 1 upvalue, 0 locals, 3 constants, 0 functions
    1    [1]    LOADK        0 -2    ; 1
    2    [1]    SETTABUP     0 -1 -3    ; _ENV "a" 2
    3    [1]    SETTABUP     0 -1 0    ; _ENV "a"
    4    [1]    RETURN       0 1
constants (3) for 0x222e0c0:
    1    "a"
    2    1
    3    2
locals (0) for 0x222e0c0:
upvalues (1) for 0x222e0c0:
    0    _ENV    1    0

On the other hand, this is actually defined:
local a,a,a = 1,2,3

Due to shadowing, this _expression_ generates 3 locals with the same name, each with a different value, and the last one takes effect. (thus, when scanning locals, you should scan them BACKWARDS, not forwards like most ppl do >.>)

$ luac -l -l -
local a,a,a = 1,2,3

main <stdin:0,0> (4 instructions at 0x14050c0)
0+ params, 3 slots, 1 upvalue, 3 locals, 3 constants, 0 functions
    1    [1]    LOADK        0 -1    ; 1
    2    [1]    LOADK        1 -2    ; 2
    3    [1]    LOADK        2 -3    ; 3
    4    [1]    RETURN       0 1
constants (3) for 0x14050c0:
    1    1
    2    2
    3    3
locals (3) for 0x14050c0:
    0    a    4    5
    1    a    4    5
    2    a    4    5
upvalues (1) for 0x14050c0:
    0    _ENV    1    0
[
-- Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.

AltStyle によって変換されたページ (->オリジナル) /