多重分配如何工作? [英] How does multiple assignment work?

查看:66
本文介绍了多重分配如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Lua编程第4.1节.

在多次分配中,Lua首先评估所有值,然后才评估 执行分配.因此,我们可以使用多重分配 交换两个值,如

In a multiple assignment, Lua first evaluates all values and only then executes the assignments. Therefore, we can use a multiple assignment to swap two values, as in

x,y = y,x-交换x' for y'

x, y = y, x -- swap x' fory'

该分配实际上如何工作?

How does the assignment work actually?

推荐答案

如何实现多个分配取决于您所使用的Lua的实现.只要保留语义,该实现就可以随意执行任何操作.也就是说,无论如何实现,如Lua书所述,在将所有值分配给LHS之前,您应该获得与将所有值保存在RHS中相同的结果.

How multiple assignment gets implemented depends on what implementation of Lua you are using. The implementation is free to do things anyway it likes as long as it preserves the semantics. That is, no matter how things get implemented, you should get the same result as if you had saved all the values in the RHS before assigning them to the LHS, as the Lua book explains.

如果您仍然对实际实现感到好奇,那么您可以做的一件事就是查看为某个程序生成的字节码是什么.例如,采用以下程序

If you are still curious about the actual implementation, one thing you can do is see what is the bytecode that gets produced for a certain program. For example, taking the following program

local x,y = 10, 11
x,y = y,x

并将其传递给Lua 5.2的字节码编译器(luac -l)给出

and passing it to the bytecode compiler (luac -l) for Lua 5.2 gives

main <lop.lua:0,0> (6 instructions at 0x9b36b50)
0+ params, 3 slots, 1 upvalue, 2 locals, 2 constants, 0 functions
    1   [1] LOADK       0 -1    ; 10
    2   [1] LOADK       1 -2    ; 11
    3   [2] MOVE        2 1
    4   [2] MOVE        1 0
    5   [2] MOVE        0 2
    6   [2] RETURN      0 1

MOVE操作码将右寄存器中的值分配给左寄存器(有关更多详细信息,请参见Lua源代码中的lopcodes.h).显然,这是因为xy使用了寄存器0和1,而插槽2被用作了临时的额外插槽.在前两个操作码中用常量初始化xy,在接下来的三个操作码中,使用第二个临时"插槽进行交换,就像您手动进行的操作一样:

The MOVE opcode assigns the value in the right register to the left register (see lopcodes.h in the Lua source for more details). Apparently, what is going on is that registers 0 and 1 are being used for x and y and slot 2 is being used as a temporary extra slot. x and y get initialized with constants in the first two opcodes and in the next three 3 opcodes a swap is performed using the "temporary" second slot, kind of like you would do by hand:

tmp = y -- MOVE 2 1
y = x   -- MOVE 1 0
x = tmp -- MOVE 0 2

鉴于Lua在执行交换分配和静态初始化时如何使用不同的方法,如果您对不同种类的多种分配获得不同的结果,我也不会感到惊讶(设置表字段可能看起来非常不同,特别是从那时起,顺序应该因元方法而重要...).我们需要在源代码中找到发出字节码的部分,但是该部分是100%确定的.正如我之前提到的,在Lua的版本和实现之间,所有这些可能会有所不同,特别是如果您查看LuaJIT与PUC Lua.

Given how Lua used a different approach when doing a swapping assignment and a static initialization, I wouldn't be surprised if you got different results for different kinds of multiple assignments (setting table fields is probably going to look very different, specially since then the order should matter due to metamethods...). We would need to find the part in the source where the bytecode gets emitted to be 100% sure though. And as I mentioned before, all of this might vary between Lua versions and implementations, specially if you look at LuaJIT vs PUC Lua.

这篇关于多重分配如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆