Skip to content

Commit 39faf5b

Browse files
committed
update lua55
1 parent 29d1bdb commit 39faf5b

File tree

10 files changed

+144
-74
lines changed

10 files changed

+144
-74
lines changed

3rd/lua55/lcode.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ void luaK_setoneret (FuncState *fs, expdesc *e) {
806806
** Change a vararg parameter into a regular local variable
807807
*/
808808
void luaK_vapar2local (FuncState *fs, expdesc *var) {
809-
fs->f->flag |= PF_VATAB; /* function will need a vararg table */
809+
needvatab(fs->f); /* function will need a vararg table */
810810
/* now a vararg parameter is equivalent to a regular local variable */
811811
var->k = VLOCAL;
812812
}
@@ -1127,7 +1127,7 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
11271127
break;
11281128
}
11291129
case VVARGIND: {
1130-
fs->f->flag |= PF_VATAB; /* function will need a vararg table */
1130+
needvatab(fs->f); /* function will need a vararg table */
11311131
/* now, assignment is to a regular table */
11321132
} /* FALLTHROUGH */
11331133
case VINDEXED: {
@@ -1927,30 +1927,37 @@ static int finaltarget (Instruction *code, int i) {
19271927
void luaK_finish (FuncState *fs) {
19281928
int i;
19291929
Proto *p = fs->f;
1930+
if (p->flag & PF_VATAB) /* will it use a vararg table? */
1931+
p->flag &= cast_byte(~PF_VAHID); /* then it will not use hidden args. */
19301932
for (i = 0; i < fs->pc; i++) {
19311933
Instruction *pc = &p->code[i];
19321934
/* avoid "not used" warnings when assert is off (for 'onelua.c') */
19331935
(void)luaP_isOT; (void)luaP_isIT;
19341936
lua_assert(i == 0 || luaP_isOT(*(pc - 1)) == luaP_isIT(*pc));
19351937
switch (GET_OPCODE(*pc)) {
19361938
case OP_RETURN0: case OP_RETURN1: {
1937-
if (!(fs->needclose || (p->flag & PF_ISVARARG)))
1939+
if (!(fs->needclose || (p->flag & PF_VAHID)))
19381940
break; /* no extra work */
19391941
/* else use OP_RETURN to do the extra work */
19401942
SET_OPCODE(*pc, OP_RETURN);
19411943
} /* FALLTHROUGH */
19421944
case OP_RETURN: case OP_TAILCALL: {
19431945
if (fs->needclose)
19441946
SETARG_k(*pc, 1); /* signal that it needs to close */
1945-
if (p->flag & PF_ISVARARG)
1946-
SETARG_C(*pc, p->numparams + 1); /* signal that it is vararg */
1947+
if (p->flag & PF_VAHID) /* does it use hidden arguments? */
1948+
SETARG_C(*pc, p->numparams + 1); /* signal that */
19471949
break;
19481950
}
19491951
case OP_GETVARG: {
19501952
if (p->flag & PF_VATAB) /* function has a vararg table? */
19511953
SET_OPCODE(*pc, OP_GETTABLE); /* must get vararg there */
19521954
break;
19531955
}
1956+
case OP_VARARG: {
1957+
if (p->flag & PF_VATAB) /* function has a vararg table? */
1958+
SETARG_k(*pc, 1); /* must get vararg there */
1959+
break;
1960+
}
19541961
case OP_JMP: { /* to optimize jumps to jumps */
19551962
int target = finaltarget(p->code, i);
19561963
fixjump(fs, i, target); /* jump directly to final target */

3rd/lua55/lcorolib.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,17 @@ static int luaB_close (lua_State *L) {
189189
return 2;
190190
}
191191
}
192-
case COS_RUN: /* running coroutine? */
192+
case COS_NORM:
193+
return luaL_error(L, "cannot close a %s coroutine", statname[status]);
194+
case COS_RUN:
193195
lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */
194196
if (lua_tothread(L, -1) == co)
195197
return luaL_error(L, "cannot close main thread");
196198
lua_closethread(co, L); /* close itself */
197-
lua_assert(0); /* previous call does not return */
199+
/* previous call does not return *//* FALLTHROUGH */
200+
default:
201+
lua_assert(0);
198202
return 0;
199-
default: /* normal or running coroutine */
200-
return luaL_error(L, "cannot close a %s coroutine", statname[status]);
201203
}
202204
}
203205

3rd/lua55/ldebug.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static const char *upvalname (const Proto *p, int uv) {
184184

185185

186186
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
187-
if (clLvalue(s2v(ci->func.p))->p->flag & PF_ISVARARG) {
187+
if (clLvalue(s2v(ci->func.p))->p->flag & PF_VAHID) {
188188
int nextra = ci->u.l.nextraargs;
189189
if (n >= -nextra) { /* 'n' is negative */
190190
*pos = ci->func.p - nextra - (n + 1);
@@ -304,7 +304,7 @@ static void collectvalidlines (lua_State *L, Closure *f) {
304304
int i;
305305
TValue v;
306306
setbtvalue(&v); /* boolean 'true' to be the value of all indices */
307-
if (!(p->flag & PF_ISVARARG)) /* regular function? */
307+
if (!(isvararg(p))) /* regular function? */
308308
i = 0; /* consider all instructions */
309309
else { /* vararg function */
310310
lua_assert(GET_OPCODE(p->code[0]) == OP_VARARGPREP);
@@ -348,7 +348,7 @@ static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,
348348
ar->nparams = 0;
349349
}
350350
else {
351-
ar->isvararg = (f->l.p->flag & PF_ISVARARG) ? 1 : 0;
351+
ar->isvararg = (isvararg(f->l.p)) ? 1 : 0;
352352
ar->nparams = f->l.p->numparams;
353353
}
354354
break;
@@ -912,7 +912,7 @@ int luaG_tracecall (lua_State *L) {
912912
Proto *p = ci_func(ci)->p;
913913
ci->u.l.trap = 1; /* ensure hooks will be checked */
914914
if (ci->u.l.savedpc == p->code) { /* first instruction (not resuming)? */
915-
if (p->flag & PF_ISVARARG)
915+
if (isvararg(p))
916916
return 0; /* hooks will start at VARARGPREP instruction */
917917
else if (!(ci->callstatus & CIST_HOOKYIELD)) /* not yielded? */
918918
luaD_hookcall(L, ci); /* check 'call' hook */

3rd/lua55/ldo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ static void rethook (lua_State *L, CallInfo *ci, int nres) {
487487
int ftransfer;
488488
if (isLua(ci)) {
489489
Proto *p = ci_func(ci)->p;
490-
if (p->flag & PF_ISVARARG)
490+
if (p->flag & PF_VAHID)
491491
delta = ci->u.l.nextraargs + p->numparams + 1;
492492
}
493493
ci->func.p += delta; /* if vararg, back to virtual 'func' */

3rd/lua55/lobject.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,18 @@ typedef struct AbsLineInfo {
583583
/*
584584
** Flags in Prototypes
585585
*/
586-
#define PF_ISVARARG 1 /* function is vararg */
587-
#define PF_VAVAR 2 /* function has vararg parameter */
588-
#define PF_VATAB 4 /* function has vararg table */
589-
#define PF_FIXED 8 /* prototype has parts in fixed memory */
586+
#define PF_VAHID 1 /* function has hidden vararg arguments */
587+
#define PF_VATAB 2 /* function has vararg table */
588+
#define PF_FIXED 4 /* prototype has parts in fixed memory */
590589

590+
/* a vararg function either has hidden args. or a vararg table */
591+
#define isvararg(p) ((p)->flag & (PF_VAHID | PF_VATAB))
592+
593+
/*
594+
** mark that a function needs a vararg table. (The flag PF_VAHID will
595+
** be cleared later.)
596+
*/
597+
#define needvatab(p) ((p)->flag |= PF_VATAB)
591598

592599
/*
593600
** Function Prototypes

3rd/lua55/lopcodes.h

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
224224

225225

226226
/*
227-
** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
228-
** has extra descriptions in the notes after the enumeration.
227+
** Grep "ORDER OP" if you change this enum.
228+
** See "Notes" below for more information about some instructions.
229229
*/
230230

231231
typedef enum {
@@ -238,7 +238,7 @@ OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
238238
OP_LOADK,/* A Bx R[A] := K[Bx] */
239239
OP_LOADKX,/* A R[A] := K[extra arg] */
240240
OP_LOADFALSE,/* A R[A] := false */
241-
OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */
241+
OP_LFALSESKIP,/*A R[A] := false; pc++ */
242242
OP_LOADTRUE,/* A R[A] := true */
243243
OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
244244
OP_GETUPVAL,/* A B R[A] := UpValue[B] */
@@ -289,7 +289,7 @@ OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
289289
OP_SHL,/* A B C R[A] := R[B] << R[C] */
290290
OP_SHR,/* A B C R[A] := R[B] >> R[C] */
291291

292-
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
292+
OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] */
293293
OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
294294
OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
295295

@@ -315,12 +315,12 @@ OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
315315
OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
316316

317317
OP_TEST,/* A k if (not R[A] == k) then pc++ */
318-
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
318+
OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] */
319319

320320
OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
321321
OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
322322

323-
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
323+
OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] */
324324
OP_RETURN0,/* return */
325325
OP_RETURN1,/* A return R[A] */
326326

@@ -336,13 +336,13 @@ OP_SETLIST,/* A vB vC k R[A][vC+i] := R[A+i], 1 <= i <= vB */
336336

337337
OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
338338

339-
OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
339+
OP_VARARG,/* A B C k R[A], ..., R[A+C-2] = varargs */
340340

341341
OP_GETVARG, /* A B C R[A] := R[B][R[C]], R[B] is vararg parameter */
342342

343-
OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx] is global name)*/
343+
OP_ERRNNIL,/* A Bx raise error if R[A] ~= nil (K[Bx - 1] is global name)*/
344344

345-
OP_VARARGPREP,/* (adjust vararg parameters) */
345+
OP_VARARGPREP,/* (adjust varargs) */
346346

347347
OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
348348
} OpCode;
@@ -371,7 +371,8 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
371371
OP_RETURN*, OP_SETLIST) may use 'top'.
372372
373373
(*) In OP_VARARG, if (C == 0) then use actual number of varargs and
374-
set top (like in OP_CALL with C == 0).
374+
set top (like in OP_CALL with C == 0). 'k' means function has a
375+
vararg table, which is in R[B].
375376
376377
(*) In OP_RETURN, if (B == 0) then return up to 'top'.
377378
@@ -386,18 +387,23 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
386387
power of 2) plus 1, or zero for size zero. If not k, the array size
387388
is vC. Otherwise, the array size is EXTRAARG _ vC.
388389
390+
(*) In OP_ERRNNIL, (Bx == 0) means index of global name doesn't
391+
fit in Bx. (So, that name is not available for the error message.)
392+
389393
(*) For comparisons, k specifies what condition the test should accept
390394
(true or false).
391395
392396
(*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
393-
(the constant is the first operand).
397+
(the constant is the first operand).
394398
395-
(*) All 'skips' (pc++) assume that next instruction is a jump.
399+
(*) All comparison and test instructions assume that the instruction
400+
being skipped (pc++) is a jump.
396401
397402
(*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
398403
function builds upvalues, which may need to be closed. C > 0 means
399-
the function is vararg, so that its 'func' must be corrected before
400-
returning; in this case, (C - 1) is its number of fixed parameters.
404+
the function has hidden vararg arguments, so that its 'func' must be
405+
corrected before returning; in this case, (C - 1) is its number of
406+
fixed parameters.
401407
402408
(*) In comparisons with an immediate operand, C signals whether the
403409
original operand was a float. (It must be corrected in case of

3rd/lua55/lparser.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static void check_readonly (LexState *ls, expdesc *e) {
304304
break;
305305
}
306306
case VVARGIND: {
307-
fs->f->flag |= PF_VATAB; /* function will need a vararg table */
307+
needvatab(fs->f); /* function will need a vararg table */
308308
e->k = VINDEXED;
309309
} /* FALLTHROUGH */
310310
case VINDEXUP: case VINDEXSTR: case VINDEXED: { /* global variable */
@@ -1056,9 +1056,8 @@ static void constructor (LexState *ls, expdesc *t) {
10561056
/* }====================================================================== */
10571057

10581058

1059-
static void setvararg (FuncState *fs, int kind) {
1060-
lua_assert(kind & PF_ISVARARG);
1061-
fs->f->flag |= cast_byte(kind);
1059+
static void setvararg (FuncState *fs) {
1060+
fs->f->flag |= PF_VAHID; /* by default, use hidden vararg arguments */
10621061
luaK_codeABC(fs, OP_VARARGPREP, 0, 0, 0);
10631062
}
10641063

@@ -1078,12 +1077,12 @@ static void parlist (LexState *ls) {
10781077
break;
10791078
}
10801079
case TK_DOTS: {
1081-
varargk |= PF_ISVARARG;
1080+
varargk = 1;
10821081
luaX_next(ls); /* skip '...' */
1083-
if (ls->t.token == TK_NAME) {
1082+
if (ls->t.token == TK_NAME)
10841083
new_varkind(ls, str_checkname(ls), RDKVAVAR);
1085-
varargk |= PF_VAVAR;
1086-
}
1084+
else
1085+
new_localvarliteral(ls, "(vararg table)");
10871086
break;
10881087
}
10891088
default: luaX_syntaxerror(ls, "<name> or '...' expected");
@@ -1092,10 +1091,9 @@ static void parlist (LexState *ls) {
10921091
}
10931092
adjustlocalvars(ls, nparams);
10941093
f->numparams = cast_byte(fs->nactvar);
1095-
if (varargk != 0) {
1096-
setvararg(fs, varargk); /* declared vararg */
1097-
if (varargk & PF_VAVAR)
1098-
adjustlocalvars(ls, 1); /* vararg parameter */
1094+
if (varargk) {
1095+
setvararg(fs); /* declared vararg */
1096+
adjustlocalvars(ls, 1); /* vararg parameter */
10991097
}
11001098
/* reserve registers for parameters (plus vararg parameter, if present) */
11011099
luaK_reserveregs(fs, fs->nactvar);
@@ -1285,9 +1283,9 @@ static void simpleexp (LexState *ls, expdesc *v) {
12851283
}
12861284
case TK_DOTS: { /* vararg */
12871285
FuncState *fs = ls->fs;
1288-
check_condition(ls, fs->f->flag & PF_ISVARARG,
1286+
check_condition(ls, isvararg(fs->f),
12891287
"cannot use '...' outside a vararg function");
1290-
init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 0, 1));
1288+
init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, fs->f->numparams, 1));
12911289
break;
12921290
}
12931291
case '{' /*}*/: { /* constructor */
@@ -2153,7 +2151,7 @@ static void mainfunc (LexState *ls, FuncState *fs) {
21532151
BlockCnt bl;
21542152
Upvaldesc *env;
21552153
open_func(ls, fs, &bl);
2156-
setvararg(fs, PF_ISVARARG); /* main function is always vararg */
2154+
setvararg(fs); /* main function is always vararg */
21572155
env = allocupvalue(fs); /* ...set environment upvalue */
21582156
env->instack = 1;
21592157
env->idx = 0;

0 commit comments

Comments
 (0)