Skip to content

Commit e927c3e

Browse files
committed
update to lua 5.4.6
1 parent 5e78f30 commit e927c3e

40 files changed

Lines changed: 997 additions & 734 deletions

luaApp/src/core/lapi.c

Lines changed: 123 additions & 126 deletions
Large diffs are not rendered by default.

luaApp/src/core/lapi.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@
1212
#include "lstate.h"
1313

1414

15-
/* Increments 'L->top', checking for stack overflows */
16-
#define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \
17-
"stack overflow");}
15+
/* Increments 'L->top.p', checking for stack overflows */
16+
#define api_incr_top(L) {L->top.p++; \
17+
api_check(L, L->top.p <= L->ci->top.p, \
18+
"stack overflow");}
1819

1920

2021
/*
2122
** If a call returns too many multiple returns, the callee may not have
2223
** stack space to accommodate all results. In this case, this macro
23-
** increases its stack space ('L->ci->top').
24+
** increases its stack space ('L->ci->top.p').
2425
*/
2526
#define adjustresults(L,nres) \
26-
{ if ((nres) <= LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; }
27+
{ if ((nres) <= LUA_MULTRET && L->ci->top.p < L->top.p) \
28+
L->ci->top.p = L->top.p; }
2729

2830

2931
/* Ensure the stack has at least 'n' elements */
30-
#define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \
31-
"not enough elements in the stack")
32+
#define api_checknelems(L,n) \
33+
api_check(L, (n) < (L->top.p - L->ci->func.p), \
34+
"not enough elements in the stack")
3235

3336

3437
/*

luaApp/src/core/lauxlib.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -526,13 +526,14 @@ static void newbox (lua_State *L) {
526526

527527
/*
528528
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
529-
** bytes.
529+
** bytes. (The test for "not big enough" also gets the case when the
530+
** computation of 'newsize' overflows.)
530531
*/
531532
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
532-
size_t newsize = B->size * 2; /* double buffer size */
533+
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
533534
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
534535
return luaL_error(B->L, "buffer too large");
535-
if (newsize < B->n + sz) /* double is not big enough? */
536+
if (newsize < B->n + sz) /* not big enough? */
536537
newsize = B->n + sz;
537538
return newsize;
538539
}
@@ -611,7 +612,7 @@ LUALIB_API void luaL_pushresultsize (luaL_Buffer *B, size_t sz) {
611612
** box (if existent) is not on the top of the stack. So, instead of
612613
** calling 'luaL_addlstring', it replicates the code using -2 as the
613614
** last argument to 'prepbuffsize', signaling that the box is (or will
614-
** be) bellow the string being added to the buffer. (Box creation can
615+
** be) below the string being added to the buffer. (Box creation can
615616
** trigger an emergency GC, so we should not remove the string from the
616617
** stack before we have the space guaranteed.)
617618
*/
@@ -739,17 +740,18 @@ static int errfile (lua_State *L, const char *what, int fnameindex) {
739740
}
740741

741742

742-
static int skipBOM (LoadF *lf) {
743-
const char *p = "\xEF\xBB\xBF"; /* UTF-8 BOM mark */
744-
int c;
745-
lf->n = 0;
746-
do {
747-
c = getc(lf->f);
748-
if (c == EOF || c != *(const unsigned char *)p++) return c;
749-
lf->buff[lf->n++] = c; /* to be read by the parser */
750-
} while (*p != '\0');
751-
lf->n = 0; /* prefix matched; discard it */
752-
return getc(lf->f); /* return next character */
743+
/*
744+
** Skip an optional BOM at the start of a stream. If there is an
745+
** incomplete BOM (the first character is correct but the rest is
746+
** not), returns the first character anyway to force an error
747+
** (as no chunk can start with 0xEF).
748+
*/
749+
static int skipBOM (FILE *f) {
750+
int c = getc(f); /* read first character */
751+
if (c == 0xEF && getc(f) == 0xBB && getc(f) == 0xBF) /* correct BOM? */
752+
return getc(f); /* ignore BOM and return next char */
753+
else /* no (valid) BOM */
754+
return c; /* return first character */
753755
}
754756

755757

@@ -760,13 +762,13 @@ static int skipBOM (LoadF *lf) {
760762
** first "valid" character of the file (after the optional BOM and
761763
** a first-line comment).
762764
*/
763-
static int skipcomment (LoadF *lf, int *cp) {
764-
int c = *cp = skipBOM(lf);
765+
static int skipcomment (FILE *f, int *cp) {
766+
int c = *cp = skipBOM(f);
765767
if (c == '#') { /* first line is a comment (Unix exec. file)? */
766768
do { /* skip first line */
767-
c = getc(lf->f);
769+
c = getc(f);
768770
} while (c != EOF && c != '\n');
769-
*cp = getc(lf->f); /* skip end-of-line, if present */
771+
*cp = getc(f); /* next character after comment, if present */
770772
return 1; /* there was a comment */
771773
}
772774
else return 0; /* no comment */
@@ -788,12 +790,16 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
788790
lf.f = fopen(filename, "r");
789791
if (lf.f == NULL) return errfile(L, "open", fnameindex);
790792
}
791-
if (skipcomment(&lf, &c)) /* read initial portion */
792-
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
793-
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
794-
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
795-
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
796-
skipcomment(&lf, &c); /* re-read initial portion */
793+
lf.n = 0;
794+
if (skipcomment(lf.f, &c)) /* read initial portion */
795+
lf.buff[lf.n++] = '\n'; /* add newline to correct line numbers */
796+
if (c == LUA_SIGNATURE[0]) { /* binary file? */
797+
lf.n = 0; /* remove possible newline */
798+
if (filename) { /* "real" file? */
799+
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
800+
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
801+
skipcomment(lf.f, &c); /* re-read initial portion */
802+
}
797803
}
798804
if (c != EOF)
799805
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */

luaApp/src/core/lcode.c

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,35 @@ static int constfolding (FuncState *fs, int op, expdesc *e1,
13511351
}
13521352

13531353

1354+
/*
1355+
** Convert a BinOpr to an OpCode (ORDER OPR - ORDER OP)
1356+
*/
1357+
l_sinline OpCode binopr2op (BinOpr opr, BinOpr baser, OpCode base) {
1358+
lua_assert(baser <= opr &&
1359+
((baser == OPR_ADD && opr <= OPR_SHR) ||
1360+
(baser == OPR_LT && opr <= OPR_LE)));
1361+
return cast(OpCode, (cast_int(opr) - cast_int(baser)) + cast_int(base));
1362+
}
1363+
1364+
1365+
/*
1366+
** Convert a UnOpr to an OpCode (ORDER OPR - ORDER OP)
1367+
*/
1368+
l_sinline OpCode unopr2op (UnOpr opr) {
1369+
return cast(OpCode, (cast_int(opr) - cast_int(OPR_MINUS)) +
1370+
cast_int(OP_UNM));
1371+
}
1372+
1373+
1374+
/*
1375+
** Convert a BinOpr to a tag method (ORDER OPR - ORDER TM)
1376+
*/
1377+
l_sinline TMS binopr2TM (BinOpr opr) {
1378+
lua_assert(OPR_ADD <= opr && opr <= OPR_SHR);
1379+
return cast(TMS, (cast_int(opr) - cast_int(OPR_ADD)) + cast_int(TM_ADD));
1380+
}
1381+
1382+
13541383
/*
13551384
** Emit code for unary expressions that "produce values"
13561385
** (everything but 'not').
@@ -1389,12 +1418,15 @@ static void finishbinexpval (FuncState *fs, expdesc *e1, expdesc *e2,
13891418
** Emit code for binary expressions that "produce values" over
13901419
** two registers.
13911420
*/
1392-
static void codebinexpval (FuncState *fs, OpCode op,
1421+
static void codebinexpval (FuncState *fs, BinOpr opr,
13931422
expdesc *e1, expdesc *e2, int line) {
1394-
int v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
1423+
OpCode op = binopr2op(opr, OPR_ADD, OP_ADD);
1424+
int v2 = luaK_exp2anyreg(fs, e2); /* make sure 'e2' is in a register */
1425+
/* 'e1' must be already in a register or it is a constant */
1426+
lua_assert((VNIL <= e1->k && e1->k <= VKSTR) ||
1427+
e1->k == VNONRELOC || e1->k == VRELOC);
13951428
lua_assert(OP_ADD <= op && op <= OP_SHR);
1396-
finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN,
1397-
cast(TMS, (op - OP_ADD) + TM_ADD));
1429+
finishbinexpval(fs, e1, e2, op, v2, 0, line, OP_MMBIN, binopr2TM(opr));
13981430
}
13991431

14001432

@@ -1410,6 +1442,18 @@ static void codebini (FuncState *fs, OpCode op,
14101442
}
14111443

14121444

1445+
/*
1446+
** Code binary operators with K operand.
1447+
*/
1448+
static void codebinK (FuncState *fs, BinOpr opr,
1449+
expdesc *e1, expdesc *e2, int flip, int line) {
1450+
TMS event = binopr2TM(opr);
1451+
int v2 = e2->u.info; /* K index */
1452+
OpCode op = binopr2op(opr, OPR_ADD, OP_ADDK);
1453+
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1454+
}
1455+
1456+
14131457
/* Try to code a binary operator negating its second operand.
14141458
** For the metamethod, 2nd operand must keep its original value.
14151459
*/
@@ -1437,24 +1481,27 @@ static void swapexps (expdesc *e1, expdesc *e2) {
14371481
}
14381482

14391483

1484+
/*
1485+
** Code binary operators with no constant operand.
1486+
*/
1487+
static void codebinNoK (FuncState *fs, BinOpr opr,
1488+
expdesc *e1, expdesc *e2, int flip, int line) {
1489+
if (flip)
1490+
swapexps(e1, e2); /* back to original order */
1491+
codebinexpval(fs, opr, e1, e2, line); /* use standard operators */
1492+
}
1493+
1494+
14401495
/*
14411496
** Code arithmetic operators ('+', '-', ...). If second operand is a
14421497
** constant in the proper range, use variant opcodes with K operands.
14431498
*/
14441499
static void codearith (FuncState *fs, BinOpr opr,
14451500
expdesc *e1, expdesc *e2, int flip, int line) {
1446-
TMS event = cast(TMS, opr + TM_ADD);
1447-
if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
1448-
int v2 = e2->u.info; /* K index */
1449-
OpCode op = cast(OpCode, opr + OP_ADDK);
1450-
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1451-
}
1452-
else { /* 'e2' is neither an immediate nor a K operand */
1453-
OpCode op = cast(OpCode, opr + OP_ADD);
1454-
if (flip)
1455-
swapexps(e1, e2); /* back to original order */
1456-
codebinexpval(fs, op, e1, e2, line); /* use standard operators */
1457-
}
1501+
if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */
1502+
codebinK(fs, opr, e1, e2, flip, line);
1503+
else /* 'e2' is neither an immediate nor a K operand */
1504+
codebinNoK(fs, opr, e1, e2, flip, line);
14581505
}
14591506

14601507

@@ -1471,61 +1518,55 @@ static void codecommutative (FuncState *fs, BinOpr op,
14711518
flip = 1;
14721519
}
14731520
if (op == OPR_ADD && isSCint(e2)) /* immediate operand? */
1474-
codebini(fs, cast(OpCode, OP_ADDI), e1, e2, flip, line, TM_ADD);
1521+
codebini(fs, OP_ADDI, e1, e2, flip, line, TM_ADD);
14751522
else
14761523
codearith(fs, op, e1, e2, flip, line);
14771524
}
14781525

14791526

14801527
/*
1481-
** Code bitwise operations; they are all associative, so the function
1528+
** Code bitwise operations; they are all commutative, so the function
14821529
** tries to put an integer constant as the 2nd operand (a K operand).
14831530
*/
14841531
static void codebitwise (FuncState *fs, BinOpr opr,
14851532
expdesc *e1, expdesc *e2, int line) {
14861533
int flip = 0;
1487-
int v2;
1488-
OpCode op;
1489-
if (e1->k == VKINT && luaK_exp2RK(fs, e1)) {
1534+
if (e1->k == VKINT) {
14901535
swapexps(e1, e2); /* 'e2' will be the constant operand */
14911536
flip = 1;
14921537
}
1493-
else if (!(e2->k == VKINT && luaK_exp2RK(fs, e2))) { /* no constants? */
1494-
op = cast(OpCode, opr + OP_ADD);
1495-
codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */
1496-
return;
1497-
}
1498-
v2 = e2->u.info; /* index in K array */
1499-
op = cast(OpCode, opr + OP_ADDK);
1500-
lua_assert(ttisinteger(&fs->f->k[v2]));
1501-
finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK,
1502-
cast(TMS, opr + TM_ADD));
1538+
if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */
1539+
codebinK(fs, opr, e1, e2, flip, line);
1540+
else /* no constants */
1541+
codebinNoK(fs, opr, e1, e2, flip, line);
15031542
}
15041543

15051544

15061545
/*
15071546
** Emit code for order comparisons. When using an immediate operand,
15081547
** 'isfloat' tells whether the original value was a float.
15091548
*/
1510-
static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
1549+
static void codeorder (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15111550
int r1, r2;
15121551
int im;
15131552
int isfloat = 0;
1553+
OpCode op;
15141554
if (isSCnumber(e2, &im, &isfloat)) {
15151555
/* use immediate operand */
15161556
r1 = luaK_exp2anyreg(fs, e1);
15171557
r2 = im;
1518-
op = cast(OpCode, (op - OP_LT) + OP_LTI);
1558+
op = binopr2op(opr, OPR_LT, OP_LTI);
15191559
}
15201560
else if (isSCnumber(e1, &im, &isfloat)) {
15211561
/* transform (A < B) to (B > A) and (A <= B) to (B >= A) */
15221562
r1 = luaK_exp2anyreg(fs, e2);
15231563
r2 = im;
1524-
op = (op == OP_LT) ? OP_GTI : OP_GEI;
1564+
op = binopr2op(opr, OPR_LT, OP_GTI);
15251565
}
15261566
else { /* regular case, compare two registers */
15271567
r1 = luaK_exp2anyreg(fs, e1);
15281568
r2 = luaK_exp2anyreg(fs, e2);
1569+
op = binopr2op(opr, OPR_LT, OP_LT);
15291570
}
15301571
freeexps(fs, e1, e2);
15311572
e1->u.info = condjump(fs, op, r1, r2, isfloat, 1);
@@ -1551,7 +1592,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15511592
op = OP_EQI;
15521593
r2 = im; /* immediate operand */
15531594
}
1554-
else if (luaK_exp2RK(fs, e2)) { /* 1st expression is constant? */
1595+
else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */
15551596
op = OP_EQK;
15561597
r2 = e2->u.info; /* constant index */
15571598
}
@@ -1568,16 +1609,16 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
15681609
/*
15691610
** Apply prefix operation 'op' to expression 'e'.
15701611
*/
1571-
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
1612+
void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
15721613
static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};
15731614
luaK_dischargevars(fs, e);
1574-
switch (op) {
1615+
switch (opr) {
15751616
case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
1576-
if (constfolding(fs, op + LUA_OPUNM, e, &ef))
1617+
if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
15771618
break;
15781619
/* else */ /* FALLTHROUGH */
15791620
case OPR_LEN:
1580-
codeunexpval(fs, cast(OpCode, op + OP_UNM), e, line);
1621+
codeunexpval(fs, unopr2op(opr), e, line);
15811622
break;
15821623
case OPR_NOT: codenot(fs, e); break;
15831624
default: lua_assert(0);
@@ -1611,7 +1652,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
16111652
case OPR_SHL: case OPR_SHR: {
16121653
if (!tonumeral(v, NULL))
16131654
luaK_exp2anyreg(fs, v);
1614-
/* else keep numeral, which may be folded with 2nd operand */
1655+
/* else keep numeral, which may be folded or used as an immediate
1656+
operand */
16151657
break;
16161658
}
16171659
case OPR_EQ: case OPR_NE: {
@@ -1706,30 +1748,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
17061748
/* coded as (r1 >> -I) */;
17071749
}
17081750
else /* regular case (two registers) */
1709-
codebinexpval(fs, OP_SHL, e1, e2, line);
1751+
codebinexpval(fs, opr, e1, e2, line);
17101752
break;
17111753
}
17121754
case OPR_SHR: {
17131755
if (isSCint(e2))
17141756
codebini(fs, OP_SHRI, e1, e2, 0, line, TM_SHR); /* r1 >> I */
17151757
else /* regular case (two registers) */
1716-
codebinexpval(fs, OP_SHR, e1, e2, line);
1758+
codebinexpval(fs, opr, e1, e2, line);
17171759
break;
17181760
}
17191761
case OPR_EQ: case OPR_NE: {
17201762
codeeq(fs, opr, e1, e2);
17211763
break;
17221764
}
1723-
case OPR_LT: case OPR_LE: {
1724-
OpCode op = cast(OpCode, (opr - OPR_EQ) + OP_EQ);
1725-
codeorder(fs, op, e1, e2);
1726-
break;
1727-
}
17281765
case OPR_GT: case OPR_GE: {
17291766
/* '(a > b)' <=> '(b < a)'; '(a >= b)' <=> '(b <= a)' */
1730-
OpCode op = cast(OpCode, (opr - OPR_NE) + OP_EQ);
17311767
swapexps(e1, e2);
1732-
codeorder(fs, op, e1, e2);
1768+
opr = cast(BinOpr, (opr - OPR_GT) + OPR_LT);
1769+
} /* FALLTHROUGH */
1770+
case OPR_LT: case OPR_LE: {
1771+
codeorder(fs, opr, e1, e2);
17331772
break;
17341773
}
17351774
default: lua_assert(0);

0 commit comments

Comments
 (0)