lua-users home
lua-l archive

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


> Can someone just confirm my thoughts that the first example is always wrong, and the second will always work.

Look at the bytecode below. You'll see that #t is computed (twice, but
from the same table) before any assignment takes place. This is what
happens in general in multiple assignments: all receiving locations are
found before the values.

% luac -l -p -
t[#t+1], t[#t+1] = 1, 2
^D

main <stdin:0,0> (12 instructions at 0x104701650)
0+ params, 5 slots, 1 upvalue, 0 locals, 3 constants, 0 functions
	1	[1]	GETTABUP 	0 0 -1	; _ENV "t"
	2	[1]	GETTABUP 	1 0 -1	; _ENV "t"
	3	[1]	LEN      	1 1
	4	[1]	ADD      	1 1 -2	; - 1
	5	[1]	GETTABUP 	2 0 -1	; _ENV "t"
	6	[1]	GETTABUP 	3 0 -1	; _ENV "t"
	7	[1]	LEN      	3 3
	8	[1]	ADD      	3 3 -2	; - 1
	9	[1]	LOADK    	4 -2	; 1
	10	[1]	SETTABLE 	2 3 -3	; - 2
	11	[1]	SETTABLE 	0 1 4
	12	[1]	RETURN   	0 1