|
On 03/01/15 02:00 PM, Zehao Jin wrote:
Let's run it thru luac $ luac -l -l - tb={ 'b' , [1] = 'a' } main <stdin:0,0> (6 instructions at 0x12220c0) 0+ params, 2 slots, 1 upvalue, 0 locals, 4 constants, 0 functions 1 [1] NEWTABLE 0 1 1 2 [1] LOADK 1 -2 ; "b" 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 $ luac -l -l - tb = { [1] = 'a' , 'b' ,[1]='c'} main <stdin:0,0> (7 instructions at 0x222d0c0) 0+ params, 2 slots, 1 upvalue, 0 locals, 5 constants, 0 functions 1 [1] NEWTABLE 0 1 2 2 [1] SETTABLE 0 -2 -3 ; 1 "a" 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 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. |