lua-users home
lua-l archive

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


2017-01-20 23:32 GMT+02:00 Patrick Donnelly <batrick@batbytes.com>:
> On Fri, Jan 20, 2017 at 3:29 AM, Marco Bambini <marco@sqlabs.net> wrote:
>> Hi all,
>> I was studying the "Closures in Lua" PDF document published some time ago.
>> The document explains the usage of the CLOSE opcode (OP_CLOSE) which has been removed in recents Lua versions.
>>
>> I wondering why this opcode has been removed.
>> It is now always used implicitly? (I guess in RETURN and JUMP but what about when a var goes out scope in a block?)
>> I think that this choice could add a lot of overhead, am I wrong?
>>
>> Thanks a lot for the explanation.
>
> There was an unused argument to the JMP instruction (A) which is now
> used (since Lua 5.2.0) to signal that closing upvalues is necessary.
> (In RETURN, it's always necessary to close any open upvalues.)
>
> See this example:
>
> $ luac -l -p -
> do
>   local a
>   local function b() return a end
> end
>
> main <stdin:0,0> (4 instructions at 0x12b5b60)
> 0+ params, 2 slots, 1 upvalue, 2 locals, 0 constants, 1 function
>         1       [2]     LOADNIL         0 0
>         2       [3]     CLOSURE         1 0     ; 0x12b5ee0
>         3       [3]     JMP             1 0     ; to 4
>         4       [4]     RETURN          0 1
>
> function <stdin:3,3> (3 instructions at 0x12b5ee0)
> 0 params, 2 slots, 1 upvalue, 0 locals, 0 constants, 0 functions
>         1       [3]     GETUPVAL        0 0     ; a
>         2       [3]     RETURN          0 2
>         3       [3]     RETURN          0 1
>
>
> compare to:
>
> $ luac -l -p -
> do
>   local a
>   local b
> end
>
> main <stdin:0,0> (2 instructions at 0x13c3b60)
> 0+ params, 2 slots, 1 upvalue, 2 locals, 0 constants, 0 functions
>         1       [2]     LOADNIL         0 1
>         2       [4]     RETURN          0 1
>
>
> No (seemingly redundant) JMP.

This gets my vote for best question and answer in a long time.