Skip to content

Commit d5a2ee7

Browse files
nwfmarcelstoer
authored andcommitted
app/lua53: catch up to lua 5.3.6 (#3415)
Adapt https://www.lua.org/work/diffs-lua-5.3.5-lua-5.3.6.html to NodeMCU. Mostly a straight application, but some small tweaks were required and, in lundump.c, some changes were elided and some additional diff reduction applied, as we have heavily diverged from upstream.
1 parent d04d024 commit d5a2ee7

File tree

10 files changed

+61
-39
lines changed

10 files changed

+61
-39
lines changed

app/lua53/host/liolib.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ static int io_popen (lua_State *L) {
277277
const char *filename = luaL_checkstring(L, 1);
278278
const char *mode = luaL_optstring(L, 2, "r");
279279
LStream *p = newprefile(L);
280+
luaL_argcheck(L, ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0'),
281+
2, "invalid mode");
280282
p->f = l_popen(L, filename, mode);
281283
p->closef = &io_pclose;
282284
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;

app/lua53/lapi.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,13 +1303,12 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
13031303
}
13041304

13051305

1306-
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1306+
static UpVal **getupvalref (lua_State *L, int fidx, int n) {
13071307
LClosure *f;
13081308
StkId fi = index2addr(L, fidx);
13091309
api_check(L, ttisLclosure(fi), "Lua function expected");
13101310
f = clLvalue(fi);
13111311
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1312-
if (pf) *pf = f;
13131312
return &f->upvals[n - 1]; /* get its upvalue pointer */
13141313
}
13151314

@@ -1318,7 +1317,7 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
13181317
StkId fi = index2addr(L, fidx);
13191318
switch (ttype(fi)) {
13201319
case LUA_TLCL: { /* lua closure */
1321-
return *getupvalref(L, fidx, n, NULL);
1320+
return *getupvalref(L, fidx, n);
13221321
}
13231322
case LUA_TCCL: { /* C closure */
13241323
CClosure *f = clCvalue(fi);
@@ -1335,9 +1334,10 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
13351334

13361335
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
13371336
int fidx2, int n2) {
1338-
LClosure *f1;
1339-
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1340-
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1337+
UpVal **up1 = getupvalref(L, fidx1, n1);
1338+
UpVal **up2 = getupvalref(L, fidx2, n2);
1339+
if (*up1 == *up2)
1340+
return;
13411341
luaC_upvdeccount(L, *up1);
13421342
*up1 = *up2;
13431343
(*up1)->refcount++;

app/lua53/lauxlib.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
651651
}
652652

653653

654-
LUALIB_API void (luaL_reref) (lua_State *L, int t, int *ref) {
654+
LUALIB_API void luaL_reref (lua_State *L, int t, int *ref) {
655655
/*
656656
* If the ref is positive and the entry in table t exists then
657657
* overwrite the value otherwise fall through to luaL_ref()
@@ -1100,8 +1100,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
11001100
free(ptr);
11011101
return NULL;
11021102
}
1103-
else
1104-
return realloc(ptr, nsize);
1103+
else { /* cannot fail when shrinking a block */
1104+
void *newptr = realloc(ptr, nsize);
1105+
if (newptr == NULL && ptr != NULL && nsize <= osize)
1106+
return ptr; /* keep the original block */
1107+
else /* no fail or not shrinking */
1108+
return newptr; /* use the new block */
1109+
}
11051110
}
11061111

11071112

app/lua53/lcode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
10591059

10601060

10611061
/*
1062-
** Aplly prefix operation 'op' to expression 'e'.
1062+
** Apply prefix operation 'op' to expression 'e'.
10631063
*/
10641064
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
10651065
static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};

app/lua53/ldebug.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
/* Active Lua function (given call info) */
3838
#define ci_func(ci) (clLvalue((ci)->func))
3939

40+
4041
static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
4142
const char **name);
4243

@@ -132,10 +133,11 @@ static const char *upvalname (Proto *p, int uv) {
132133

133134
static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
134135
int nparams = getnumparams(clLvalue(ci->func)->p);
135-
if (n >= cast_int(ci->u.l.base - ci->func) - nparams)
136+
int nvararg = cast_int(ci->u.l.base - ci->func) - nparams;
137+
if (n <= -nvararg)
136138
return NULL; /* no such vararg */
137139
else {
138-
*pos = ci->func + nparams + n;
140+
*pos = ci->func + nparams - n;
139141
return "(*vararg)"; /* generic name for any vararg */
140142
}
141143
}
@@ -147,7 +149,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
147149
StkId base;
148150
if (isLua(ci)) {
149151
if (n < 0) /* access to vararg values? */
150-
return findvararg(ci, -n, pos);
152+
return findvararg(ci, n, pos);
151153
else {
152154
base = ci->u.l.base;
153155
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
@@ -707,7 +709,6 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
707709
CallInfo *ci = L->ci;
708710
const char *msg;
709711
va_list argp;
710-
711712
luaC_checkGC(L); /* error message uses memory */
712713
va_start(argp, fmt);
713714
msg = luaO_pushvfstring(L, fmt, argp); /* format message */

app/lua53/llex.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,24 +256,27 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) {
256256

257257

258258
/*
259-
** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return
260-
** its number of '='s; otherwise, return a negative number (-1 iff there
261-
** are no '='s after initial bracket)
259+
** reads a sequence '[=*[' or ']=*]', leaving the last bracket.
260+
** If sequence is well formed, return its number of '='s + 2; otherwise,
261+
** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...').
262262
*/
263-
static int skip_sep (LexState *ls) {
264-
int count = 0;
263+
static size_t skip_sep (LexState *ls) {
264+
size_t count = 0;
265265
int s = ls->current;
266266
lua_assert(s == '[' || s == ']');
267267
save_and_next(ls);
268268
while (ls->current == '=') {
269269
save_and_next(ls);
270270
count++;
271271
}
272-
return (ls->current == s) ? count : (-count) - 1;
272+
return (ls->current == s) ? count + 2
273+
: (count == 0) ? 1
274+
: 0;
275+
273276
}
274277

275278

276-
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
279+
static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
277280
int line = ls->linenumber; /* initial line (for error message) */
278281
save_and_next(ls); /* skip 2nd '[' */
279282
if (currIsNewline(ls)) /* string starts with a newline? */
@@ -307,8 +310,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
307310
}
308311
} endloop:
309312
if (seminfo)
310-
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
311-
luaZ_bufflen(ls->buff) - 2*(2 + sep));
313+
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
314+
luaZ_bufflen(ls->buff) - 2 * sep);
312315
}
313316

314317

@@ -456,9 +459,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
456459
/* else is a comment */
457460
next(ls);
458461
if (ls->current == '[') { /* long comment? */
459-
int sep = skip_sep(ls);
462+
size_t sep = skip_sep(ls);
460463
luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
461-
if (sep >= 0) {
464+
if (sep >= 2) {
462465
read_long_string(ls, NULL, sep); /* skip long comment */
463466
luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
464467
break;
@@ -470,12 +473,12 @@ static int llex (LexState *ls, SemInfo *seminfo) {
470473
break;
471474
}
472475
case '[': { /* long string or simply '[' */
473-
int sep = skip_sep(ls);
474-
if (sep >= 0) {
476+
size_t sep = skip_sep(ls);
477+
if (sep >= 2) {
475478
read_long_string(ls, seminfo, sep);
476479
return TK_STRING;
477480
}
478-
else if (sep != -1) /* '[=...' missing second bracket */
481+
else if (sep == 0) /* '[=...' missing second bracket */
479482
lexerror(ls, "invalid long string delimiter", TK_STRING);
480483
return '[';
481484
}

app/lua53/lobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
274274
** - 'n'/'N' means 'inf' or 'nan' (which should be rejected)
275275
** - '.' just optimizes the search for the common case (nothing special)
276276
** This function accepts both the current locale or a dot as the radix
277-
** mark. If the convertion fails, it may mean number has a dot but
277+
** mark. If the conversion fails, it may mean number has a dot but
278278
** locale accepts something else. In that case, the code copies 's'
279279
** to a buffer (because 's' is read-only), changes the dot to the
280280
** current locale radix mark, and tries to convert again.

app/lua53/lparser.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
545545
fs->bl = NULL;
546546
f = fs->f;
547547
f->source = ls->source;
548+
luaC_objbarrier(ls->L, f, f->source);
548549
f->maxstacksize = 2; /* registers 0/1 are always valid */
549550
f->lineinfo = 0;
550551
fs->sizelineinfo = 0;
@@ -1626,6 +1627,7 @@ static void mainfunc (LexState *ls, FuncState *fs) {
16261627
fs->f->is_vararg = 1; /* main function is always declared vararg */
16271628
init_exp(&v, VLOCAL, 0); /* create and... */
16281629
newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */
1630+
luaC_objbarrier(ls->L, fs->f, ls->envn);
16291631
luaX_next(ls); /* read first token */
16301632
statlist(ls); /* parse main body */
16311633
check(ls, TK_EOS);
@@ -1651,6 +1653,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
16511653
sethvalue(L, L->top, lexstate.h); /* anchor it */
16521654
luaD_inctop(L);
16531655
funcstate.f = cl->p = luaF_newproto(L);
1656+
luaC_objbarrier(L, cl, cl->p);
16541657
funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
16551658
lua_assert(iswhite(funcstate.f)); /* do not need barrier here */
16561659
lexstate.buff = buff;

app/lua53/lua.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#define LUA_VERSION_MAJOR "5"
2020
#define LUA_VERSION_MINOR "3"
2121
#define LUA_VERSION_NUM 503
22-
#define LUA_VERSION_RELEASE "5"
22+
#define LUA_VERSION_RELEASE "6"
2323

2424
#define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
2525
#define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
26-
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio"
26+
#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio"
2727
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
2828

2929

@@ -508,7 +508,7 @@ LUALIB_API void (lua_debugbreak)(void);
508508
/* }====================================================================== */
509509

510510
/******************************************************************************
511-
* Copyright (C) 1994-2018 Lua.org, PUC-Rio.
511+
* Copyright (C) 1994-2020 Lua.org, PUC-Rio.
512512
*
513513
* Permission is hereby granted, free of charge, to any person obtaining
514514
* a copy of this software and associated documentation files (the

app/lua53/lundump.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,23 @@ static lua_Integer LoadInteger (LoadState *S, lu_byte tt_data) {
210210
return (tt_data & LUAU_TMASK) == LUAU_TNUMNINT ? -x-1 : x;
211211
}
212212
static TString *LoadString_ (LoadState *S, int prelen) {
213-
TString *ts;
214-
char buff[LUAI_MAXSHORTLEN];
213+
lua_State *L = S->L;
215214
int n = LoadInteger(S, (prelen < 0 ? LoadByte(S) : prelen)) - 1;
215+
TString *ts;
216216
if (n < 0)
217217
return NULL;
218218
if (S->useStrRefs)
219219
ts = S->TS[n];
220220
else if (n <= LUAI_MAXSHORTLEN) { /* short string? */
221+
char buff[LUAI_MAXSHORTLEN];
221222
LoadVector(S, buff, n);
222-
ts = luaS_newlstr(S->L, buff, n);
223+
ts = luaS_newlstr(L, buff, n);
223224
} else { /* long string */
224-
ts = luaS_createlngstrobj(S->L, n);
225+
ts = luaS_createlngstrobj(L, n);
226+
setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */
227+
luaD_inctop(L);
225228
LoadVector(S, getstr(ts), n); /* load directly in final place */
229+
L->top--; /* pop string */
226230
}
227231
return ts;
228232
}
@@ -299,8 +303,11 @@ static void LoadProtos (LoadState *S, Proto *f) {
299303
f->p = p;
300304
f->sizep = n;
301305
memset (p, 0, n * sizeof(*p));
302-
for (i = 0; i < n; i++)
303-
p[i] = LoadFunction(S, luaF_newproto(S->L), f->source);
306+
for (i = 0; i < n; i++) {
307+
p[i] = luaF_newproto(S->L);
308+
luaC_objbarrier(S->L, f, p[i]);
309+
p[i] = LoadFunction(S, p[i], f->source);
310+
}
304311
if (S->mode != MODE_RAM) {
305312
f->p = StoreAV(S, cast(void **, p), n);
306313
luaM_freearray(S->L, p, n);
@@ -438,7 +445,8 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) {
438445
setclLvalue(L, L->top, cl);
439446
luaD_inctop(L);
440447
cl->p = luaF_newproto(L);
441-
LoadFunction(&S, cl->p, NULL);
448+
luaC_objbarrier(L, cl, cl->p);
449+
cl->p = LoadFunction(&S, cl->p, NULL);
442450
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
443451
return cl;
444452
}

0 commit comments

Comments
 (0)