[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Why 'while' and 'repeat' loop need double block
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 9 Jan 2013 19:29:08 +0200
2013/1/9 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> When parsing 'while' and 'repeat' statement, lua uses double block
>> to encapsulate the inner statement(one for 'loop').
>
> If I remember correctly, the inner block controls the scope of variables,
> while the outer block controls where to jump from a break. Consider
> this code:
>
> while 1 do
> local a
> x = function () return a end
> end
>
> and its opcodes:
>
> 1 [2] LOADNIL 0 0
> 2 [3] CLOSURE 1 0 ; 0x9f18fa0
> 3 [3] MOVE 0 0
> 4 [3] SETGLOBAL 1 -1 ; x
> 5 [3] CLOSE 0
> 6 [3] JMP -6 ; to 1
> 7 [4] RETURN 0 1
>
In Lua 5.2, the code is:
1 [2] LOADNIL 0 0
2 [3] CLOSURE 1 0 ; 0x9198c18
3 [3] SETTABUP 0 -1 1 ; _ENV "x"
4 [3] JMP 1 -4 ; to 1
5 [3] JMP 0 -5 ; to 1
6 [4] RETURN 0 1
The second JMP (never reached) does look mysterious…