Skip to content

Instantly share code, notes, and snippets.

@luxuia
Created March 15, 2018 09:14
Show Gist options
  • Select an option

  • Save luxuia/e67f0bb57da6970d4b8b1e7e86654ad9 to your computer and use it in GitHub Desktop.

Select an option

Save luxuia/e67f0bb57da6970d4b8b1e7e86654ad9 to your computer and use it in GitHub Desktop.
lua 备忘
//http://www.cnblogs.com/zxh1210603696/category/682573.html
/*
** check whether, in an assignment to an upvalue/local variable, the
** upvalue/local variable is begin used in a previous assignment to a
** table. If so, save original upvalue/local value in a safe place and
** use this safe copy in the previous assignment.
*/
static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
FuncState *fs = ls->fs;
int extra = fs->freereg; /* eventual position to save local variable */
int conflict = 0;
for (; lh; lh = lh->prev) { /* check all previous assignments */
if (lh->v.k == VINDEXED) { /* assigning to a table? */
/* table is the upvalue/local being assigned now? */
if (lh->v.u.ind.vt == v->k && lh->v.u.ind.t == v->u.info) {
conflict = 1;
lh->v.u.ind.vt = VLOCAL;
lh->v.u.ind.t = extra; /* previous assignment will use safe copy */
}
/* index is the local being assigned? (index cannot be upvalue) */
if (v->k == VLOCAL && lh->v.u.ind.idx == v->u.info) {
conflict = 1;
lh->v.u.ind.idx = extra; /* previous assignment will use safe copy */
}
}
}
if (conflict) {
/* copy upvalue/local value to a temporary (in position 'extra') */
OpCode op = (v->k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;
luaK_codeABC(fs, op, extra, v->u.info, 0);
luaK_reserveregs(fs, 1);
}
}
/*
local a
a,a = 1,2
print(a)
main <test.lua:0,0> (8 instructions at 0x7f846f403490)
0+ params, 3 slots, 1 upvalue, 1 local, 3 constants, 0 functions
1 [1] LOADNIL 0 0
2 [2] LOADK 1 -1 ; 1 // 会多用一个临时变量
3 [2] LOADK 0 -2 ; 2
4 [2] MOVE 0 1
5 [3] GETTABUP 1 0 -3 ; _ENV "print"
6 [3] MOVE 2 0
7 [3] CALL 1 2 1
8 [3] RETURN 0 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment